Display Posts from Specific Category On A WordPress Page

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

In WordPress, you can categorize the posts. This feature is useful to find out all posts that come under a specific category. When the user clicks on one of the categories they will see posts assigned to 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 as per category, WordPress searches for template files in the above order. Whichever template file is found first, code from that file is executed to display posts of a category.

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

We can achieve it by writing some pieces of code. Let’s go through the steps below to accomplish this task.

Create Page Template in WordPress

We want to display posts by category on a page. So, create a file template-category.php in your active theme’s directory and add the below comment at the top of the file.

template-category.php

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

Adding this comment tells WordPress to use this template globally on any page to which this template is assigned.

Next, go to your WordPress dashboard, and 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 way to assign a template file to your WordPress page. In that case, you have to create a file in your theme in the format of page-{slug}.php. Let’s say the 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 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 that came 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;
    wp_reset_postdata();
endif;

Here, I used the 'category_name' => 'wordpress'. The ‘wordpress’ is the slug of a category. Adjust this slug as per your requirement.

Find Slug Of A Category

The user can also pass the category id instead of the name of a category. 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. Pass the -1 if you want to fetch all posts. Go through the documentation to understand more about the parameters.

Next, using WordPress Loop we go through each post and display it one by one.

Note: Though I am referring to a custom page template, you can use this code anywhere on your WordPress website to display category posts.

Get Posts from Custom Taxonomy

You may be working with a custom post type and wish to display the posts from a custom taxonomy. It can be done easily using the tax_query. For that, you just 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 the code. The above arguments will fetch the posts from a given 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, 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 have 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;

That being said our final code will be 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,
            )
        );
        wp_reset_postdata();
    endif;
    ?>
 
    </main><!-- .site-main -->
</div><!-- .content-area -->
 
<?php get_footer(); ?>

That’s it! I have created a basic page for the post listing. You can create a nicely crafted page for your post listing by applying some CSS 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.

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

  1. Subject: Lack of Built-In Functionality to Display Posts by Category in WordPress

    I recently came across your article on displaying posts from a specific category on a WordPress page using a custom template. While the solution you provided is helpful, it raises a concern about the lack of built-in functionality for such a basic requirement in WordPress.

    It seems counterintuitive that WordPress, a powerful and widely-used content management system, does not have a straightforward way to display posts assigned to a particular category without the need for coding or custom templates. This feature would greatly benefit users who want to organize their content and provide a better user experience for their visitors.

    Additionally, I noticed elsewhere that people mention the Category widget as a possible built-in solution. However, I have encountered an issue where the widget displays “No Categories” even when categories are properly assigned to posts. It appears that the widget falls short of its intended functionality.

    I believe that incorporating a built-in feature to display posts by category would enhance the usability and accessibility of WordPress for users of all skill levels. It would streamline the process and eliminate the need for custom development or relying on third-party plugins. I tried a couple of plugins that were supposed to do this, and they also did not work.

    I wanted to raise this concern and spark a discussion about this topic. I would love to hear your thoughts and insights on why this functionality is not included by default and if there are any plans to address it in future WordPress updates.

    Thank you for your informative article, and I look forward to hearing from you and the community.

    Best regards
    Mike

    1. Hi Mike, the WP_Query class provides built-in functionality to fetch posts by category. It gives you an array of posts that you can utilize the way to want to match your website.

  2. Are there anyway to update to list them either descending or ascneding?
    Also, are there ways to number the posts?
    Would greatly appreciate if anyone can help me with this..

    1. Do you mean get posts from multiple categories (this is what this article is about)? You can pass comma-separated values to ‘cat’ => ‘4,5’ like this.

      1. Yes, But I am not sure how to show it like this:
        e.g. if I would like to show cate 4 in one tab, cat 5 in another tab

        Thanks again!

    1. You can use the following in $args array
      // Posts from the tag named “Meta Query” or “Custom Taxonomy”
      $args = array(
      ‘tag’ => ‘meta-query,custom-taxonomy’
      );

      Or

      // Posts from tag 42, 38 and 55
      $args = array(
      ‘tag__in’ => array( 42, 38, 55 )
      );

  3. 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!!

  4. 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.

  5. 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.

  6. 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 .

  7. 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.

  8. 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?

  9. 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 *