2017-05-16 144 views
1

你能指导我如何在页脚上显示某些类别的帖子。我正在使用自定义帖子类型的新闻网站,分类法是新闻类别。在页脚上显示自定义帖子类型的帖子

链接结构是abc.com/news-category/politics-news。政治新闻是类别页面,所有与政治相关的新闻显示在类别页面上。

我不知道如何在页脚上显示5个相同类别的最新帖子。

我试着用tag_id但没有显示。

我也有试过这种相关的职位Related post但没有奏效

能否请您指导我

感谢

回答

2

您可以通过以下逻辑得到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查询

+0

感谢您的快速回复。什么是taxonomy_name政治新闻? – Atif

+0

不,您可以通过访问该URL来找到它,如:/wp-admin/edit-tags.php?taxonomy=category。 在你的情况下,转到该自定义类别,你可以找到它。 –

+0

嗨艾哈迈德, 我已经改变了与新闻类别的分类也改变了post_type =>'新网站',没有发生 – Atif

相关问题