请您指导我如何在页脚上显示某些类别的帖子。我使用的是自定义post类型的新闻站点,分类是新闻类别.
链接结构是abc.com/新闻-类别/政治-新闻。政治新闻是一个类别页面,所有与政治相关的新闻都显示在分类页面上。
我不知道如何显示5个最近的帖子与相同类别的页脚。
我试过用tag_id,但什么也没有显示出来。
此外,我也尝试过这个相关的帖子Related post,但没有工作
你能指点我吗?
谢谢
发布于 2017-05-16 12:47:23
您可以使用以下逻辑获得自定义分类法的5篇文章。
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
$term_id = esc_html( $categories[0]->term_id );
}
$args = array(
'post_type' => 'news-site',
'post_status' => 'publish',
'posts_per_page' => 5,
'tax_query' => array(
array(
'taxonomy' => 'news-category',
'field' => 'id',
'terms' => $term_id
)
)
);
$the_query = new WP_Query( $args );
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_title();
endwhile;
?>不要忘记在查询中传递taxonomy_name
https://stackoverflow.com/questions/44001828
复制相似问题