五、嵌入RSS阅读器
//This file is needed to be able to use the wp_rss() function. include_once(ABSPATH.WPINC.'/rss.php'); function readRss($atts) { extract(shortcode_atts(array( "feed" => 'http://', "num" => '1', ), $atts)); return wp_rss($feed, $num); } add_shortcode('rss', 'readRss'); |
使用简码的时候输入:[rss feed=“http://feed.happyet.org” num=“5”]
feed属性(attribute)即是要嵌入的feed URL,num即是要显示的条目数量。
六、使用简码从WordPress数据库中提取文章
function sc_liste( $atts, $content = null) {extract( shortcode_atts( array( "num" => '5',"cat" => ''), $atts)); global $post; $myposts = get_posts( 'numberposts='.$num.'&order=DESC&orderby=post_date&category='.$cat); $retour='
; foreach( $myposts as $post) :setup_postdata( $post); $retour.='
.get_permalink().'">'.the_title( "","",false).''; endforeach; $retour.=''; return $retour; }add_shortcode( "list", "sc_liste"); |
在WordPress编辑器中使用以下简码:[liste num=“3” cat=“1”],系统将从ID为1的类别中提取3篇文章。
代码注释:系统提取参数并创建全局变量$posts后,sc_liste()函数使用了 get_posts(),numberposts, order, orderby和category参数以从类别Y中获取X篇最新日志。完成后,系统就会以无序的HTML列表形式显示日志。
七、获取日志中的最新图像
function sc_postimage( $atts, $content = null) {extract( shortcode_atts( array( "size" => 'thumbnail',"float" => 'none'), $atts)); $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() ); foreach( $images as $imageID => $imagePost ) $fullimage = wp_get_attachment_image( $imageID, $size, false); $imagedata = wp_get_attachment_image_src( $imageID, $size, false); $width = ( $imagedata[1]+2); $height = ( $imagedata[2]+2); return '
.$width.'px; height: '.$height.'px; float: '.$float.';">'.$fullimage.' '; }add_shortcode( "postimage", "sc_postimage"); |
使用代码:[postimage size=“” float=“left”]
代码注释:sc_postimage()函数首先提取了简码属性,然后它使用get_children(), wp_get_attachment_image() 和wp_get_attachment_image_src()这些WordPress函数检索图像。完成后,系统就会返回图像并插入到文章内容中。
八、在侧边栏微件中添加简码
add_filter('widget_text', 'do_shortcode');
好是好,function.php文件得爆炸了……不知道会不会对速度有影响。 |