2017-05-16 143 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

相關問題