下面是一个简单的短代码,它可以处理分类法、帖子类型和WP\\U查询采用的任何其他参数:
add_shortcode(\'posts\',\'posts_shortcode_handler\');
function posts_shortcode_handler($atts, $content){
extract(shortcode_atts(array(
\'posts_per_page\' => \'5\',
), $atts));
global $post;
$temp = $post;
$posts = new WP_Query($atts);
$retVal = \'\';
if ($posts->have_posts()){
while ($posts->have_posts()){
$posts->the_post();
// these arguments will be available from inside $content
$parameters = array(
\'PERMALINK\' => get_permalink(),
\'TITLE\' => get_the_title(),
\'CONTENT\' => get_the_content(),
\'CATEGORIES\' => get_the_category_list(\', \'),
\'THUMBNAIL\' => get_the_post_thumbnail()
);
$finds = $replaces = array();
foreach($parameters as $find => $replace){
$finds[] = \'{\'.$find.\'}\';
$replaces[] = $replace;
}
$retVal .= str_replace($finds, $replaces, $content);
}
}
wp_reset_query();
$post = $temp;
return $retVal;
}
用法:
[posts post_type="page" posts_per_page=5 taxonomy_name="taxonomy_term"]
<h5><a href="{PERMALINK}">{TITLE}</a></h5>
<div>{THUMBNAIL} <br />{CONTENT}</div>
[/posts]
将页面替换为帖子类型名称,将taxonomy\\u name替换为taxonomy名称,将taxonomy\\u term替换为taxonomy term