Atif
Atif

Reputation: 65

Show custom post type category posts on the footer

Can you please guide me how to show certain category posts on the footer. I am using custom post type news-site, taxonomy is news-category.

The link structure is abc.com/news-category/politics-news. The Politics news is a category page and all politics related news showing on the category page.

I don't know how to show 5 recent posts with same category on the footer.

I have tried with tag_id but nothing is showing.

Also i have tried this related post Related post but didn't work

Can you please guide me

Thanks

Upvotes: 1

Views: 904

Answers (1)

Ahmed Ginani
Ahmed Ginani

Reputation: 6650

You can get the 5 posts of custom taxonomy by using the following logic.

<?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;
    ?>

Don't forget to pass taxonomy_name in the query

Upvotes: 2

Related Questions