Display Posts from Specific Category On A WordPress Page

Do you want to display posts from a specific category on a custom page template in WordPress? Using a page template, you can use your own design to display the post listing on a WordPress page. In this article, we study how to display posts from a specific category on a custom page template.

In WordPress, you can categorize your post. This feature is useful to find out all posts that come under a specific category. When a user clicks on one of the categories they can see a post listing page for that specific category. WordPress uses the following template files for rendering posts of a category.

  • category-slug.php
  • category-ID.php
  • category.php
  • archive.php
  • index.php

To display posts of the specific category, WordPress searches for template files in the above order. Whichever template file found first, code from that file will be used to display posts listing of a category.

It’s all about the default WordPress templates. But, what if someone wants to use a custom page template to list the posts of a specific category?

Let’s go through the steps below to achieve this goal.

Create Page Template in WordPress

Our goal is to display posts by category on a page. Obviously, you need to write a code for it. Create a file template-category.php in your active theme’s directory and add the below comment at the top of a file.

template-category.php

<?php
/**
 * Template Name: Category Custom Page
 */
 

Next, go to your WordPress dashboard, create the page where you want to display posts. Assign the above template to this newly created page.

Assign Template To Page

There is another method to assign a template file to your WordPress page. In that case, you have to create a file in your theme in the format page-{slug}.php. Let’s say slug of your page is category-list then your file name would be page-category-list.php. After this, the code from this file is being used for your page.

Display Posts from Specific Category on a WordPress Page

You have created and assigned your template to the WordPress page. Now, let’s write a code that fetches posts attached to a category.

I will use WP_Query class to fetch the posts. For instance, I assume you have a category called ‘WordPress’ and you want to display posts attached to this category.

Write the code below to get a list of posts under the ‘WordPress’ category.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'wordpress',
    'posts_per_page' => 5,
);
$arr_posts = new WP_Query( $args );
 
if ( $arr_posts->have_posts() ) :
 
    while ( $arr_posts->have_posts() ) :
        $arr_posts->the_post();
        ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
            <?php
            if ( has_post_thumbnail() ) :
                the_post_thumbnail();
            endif;
            ?>
            <header class="entry-header">
                <h1 class="entry-title"><?php the_title(); ?></h1>
            </header>
            <div class="entry-content">
                <?php the_excerpt(); ?>
                <a href="<?php the_permalink(); ?>">Read More</a>
            </div>
        </article>
        <?php
    endwhile;
endif;

In the above code, I used the 'category_name' => 'wordpress'. Here ‘wordpress’ is the slug of a category.

Find Slug Of A Category

The user can also pass the category id instead of category_name. In that case, you should use the key as ‘cat’ in place of ‘category_name’ and pass the id of a category.

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'cat' => '4', //you can pass comma-separated ids here
    'posts_per_page' => 5,
);

The value for posts_per_page is the number of posts to fetch from the database. I have used the have_posts() method which loops through each post and displays it.

Get Posts from Custom Taxonomy

You may be working with a custom post type and wish to display the posts from a custom taxonomy. For that, you need to change arguments in the previous array as follows:

$args = array(
    'post_type' => 'CUSTOM_POST_TYPE_NAME',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'tax_query' => array(
        array(
            'taxonomy' => 'TAXONOMY_NAME',
            'field'    => 'slug',
            'terms'    => array( 'TERM_SLUG' ),
            'operator' => 'IN'
        ),
    ),
);
 
$arr_posts = new WP_Query( $args );

No need to change the other part of code. The above arguments will get the posts of a specified taxonomy.

Pagination

The code I have written only fetches limited posts from the category. Although you can get any number of posts by passing a number to ‘posts_per_page’, we usually display limited posts per page and then use pagination to get the next and previous set of posts.

In order to integrate the pagination, you need to install and activate the WP-PageNavi plugin. This plugin provides a method wp_pagenavi() that generates pagination links like the below screenshot. These links will be used to see the next or previous posts.

Paginate Links

To add these pagination links to your WordPress page, you need to modify your code. First, you need to pass paged parameter and then use the function wp_pagenavi().

You will get the value for paged key as follows:

$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;

Our final code is as follows.

template-category.php

<?php
/**
 * Template Name: Category Custom Page
 */
 
get_header(); ?>
 
<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">
 
    <?php
    $paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
    $args = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category_name' => 'wordpress',
        'posts_per_page' => 5,
        'paged' => $paged,
    );
    $arr_posts = new WP_Query( $args );
 
    if ( $arr_posts->have_posts() ) :
 
        while ( $arr_posts->have_posts() ) :
            $arr_posts->the_post();
            ?>
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <?php
                if ( has_post_thumbnail() ) :
                    the_post_thumbnail();
                endif;
                ?>
                <header class="entry-header">
                    <h1 class="entry-title"><?php the_title(); ?></h1>
                </header>
                <div class="entry-content">
                    <?php the_excerpt(); ?>
                    <a href="<?php the_permalink(); ?>">Read More</a>
                </div>
            </article>
            <?php
        endwhile;
        wp_pagenavi(
            array(
                'query' => $arr_posts,
            )
        );
    endif;
    ?>
 
    </main><!-- .site-main -->
</div><!-- .content-area -->
 
<?php get_footer(); ?>

We have created a basic page for our listing. You can create a nicely crafted page for your posts listing by applying styling to it.

I hope you understand how to display posts from a specific category on a WordPress page. Please share your thoughts and suggestions in the comment section below.

Related Articles

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

33 thoughts on “Display Posts from Specific Category On A WordPress Page

  1. I’ve been searching for this solution for weeks. I finally typed in just the right search terms and it first came up with the List Category Posts plugin but I’m trying to modify an old theme and want it to be just in the theme and you have totally solved my problem. Thank you so much. I do need to re-read it to see how I can pass the category from the URI to use that. I’m sure I can figure it out but this was a lifesaver.

    Thanks so much!!

  2. Works for me. thanks!
    But if I need to filter many categories on respective pages I have to make this template code for each one?
    It would be better if one can use the same theme code with category name or id send as parameter.

  3. Great article! Here’s my problem: I have 7 different categories with 7 different pages. 1 page for each category. Within the page I’d like to show all posts listed under that category. How would I go about doing that? Thanks.

    1. It seems you are talking about custom post type and their categories. In that case, you need to use tax_query for custom post types.

  4. Hi I’m want to display particular category post into sidebar and sidebar change with category list on every page please suggest any idea if possible I want to create offline reading website with WordPress .

  5. The only issue that I have with this code is that the Read More button is linking to the url of the page itself, not the link of the individual posts. Any tips on how to correct this?

    1. It seems you added Read More outside of while loop. It should be inside while loop so a link will go to the individual post.

      1. I am using the exact code from this post, simply copy and pasted. Your Read More button appears inside of the while loop, as does mine. All of the information is accurately pulled from the individual post, except for the permalink.

  6. I’m just trying to figure where I place this file? Does it go under the wp-includes folder? How do I create text above the posts on the page? Like giving a title or a banner image to the page. It looks like it will just load all the posts for that particular category. Can I customize?

  7. I am using the code above and it is working well, however the page is showing the posts, but does not display the ‘read more’ link, so there is no way to go into the post to read the full post. How do I enable the link to ‘read more’?

Leave a Reply

Your email address will not be published. Required fields are marked *