在页脚上显示自定义帖子类型类别帖子

时间:2017-05-16 12:43:56

标签: php wordpress

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

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

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

我尝试过使用tag_id但没有显示任何内容。

我也试过这篇相关的帖子Related post但是没有用

你能指导我吗

由于

1 个答案:

答案 0 :(得分: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

相关问题