【发布时间】:2016-07-24 12:55:15
【问题描述】:
我试图为自定义分类术语动态创建简码。但未能如愿。 假设,如果有一个名为“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 cat="183"]
任何帮助都会非常感激。
谢谢!
【问题讨论】: