WordPress短代码术语
问题描述:
我试图动态地为自定义分类术语创建简码。但是没有这样做。 假设,如果有一个名为“wordpress”的术语,那么我应该能够通过简码查询与该术语相关的所有帖子。 更准确地说,假设有一个称为“事件”的分类法,并且在该分类法下有多个术语。所以,我试图通过每个术语的简码来查询每个术语下的帖子。WordPress短代码术语
这里是我的尝试:
function wordpress_recent_post($atts, $content) {
$a = shortcode_atts(array(
'cat' => '',
), $atts);
$args = array(
'posts_per_page' => 1,
'offset' => 0,
'category_name' => $a['cat'],
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
);
$recent_posts = new WP_Query($args);
ob_start();
if (! $recent_posts-> have_posts()) {
return 'No Posts Found for ' . $a['cat'];
}
while ($recent_posts->have_posts()) {
$recent_posts->the_post();
the_title('<h2>', '</h2>');
if ('' != $a['cat']) {
$href = '/category/' . $a['cat'];
} else {
$href = '/blog';
}
echo "<p><a href='$href'>Read More" . ucwords($a['cat']) . '</a></p>';
}
wp_reset_query();
return ob_get_clean();
}
add_shortcode('wordpress_recent_post', array($this, 'wordpress_recent_post'));
然后我用这个来调用一个叫“特色”,其ID是“183”(想) [wordpress_recent_post猫长期职位=“183” ]
任何帮助将非常可观。
谢谢!
答
添加term slug做到了。应该有slug不是id,像这样: [wordpress_recent_post cat =“features”]