Display Posts From Specific Category On A WordPress Page

Do you need to display posts from a specific category on a custom page template? Doing so, you can use your own template design for post listing on a WordPress page. In this article, we study how to display posts from a specific category on a custom page template.

By default in WordPress, you can categorize your post. This feature is useful to find out in which category your posts belongs to. In WordPress, when a user clicks on one of the categories they will redirect to post listing page for that specific category. WordPress uses the following template files for posts listing of a category.

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

To display posts, WordPress search for template files in the above order. Whichever template file found first, that files code would use for category’s posts listing.

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

Let’s go through step by step guide to achieve our goal.

Create Page Template In WordPress

Our goal is to display posts by the category on a page. Obviously, we need to write a code for it.

Create a file template-category.php in your themes directory and add the below comment at the top of file.

template-category.php

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

?>

Next, go to your WordPress dashboard, create your page where you want to display posts. Assign our template to this page.

Assign Template To Page

Display Posts From Specific Category

We have assigned our template to the WordPress page. Next thing needs to do is write a code which fetches posts attached to a category.

We will use WP_Query class to fetch the posts. For instance, we assume you have a category called ‘WordPress’ which posts you need to display.

$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 we have passed 'category_name' => 'wordpress'. Here ‘wordpress’ is the slug of a category.

Find Slug Of A Category

We can also pass category id instead of category_name.

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

posts_per_page is the number of posts to fetch from the database. We have used have_posts() method which checks whether posts are available for WordPress loop. If posts are available then we loop through each post and display it.

Pagination

The code we have written only fetches limited posts from the category. But, what if we have assigned more posts to the category. In that case, we need a paginate links.

For pagination you need to install and activate the plugin WP-PageNavi.

This plugin provides a method wp_pagenavi() which generates a paginate links on the posts listing page.

Paginate Links

To add paginate links in our WordPress page, we need to modify our code. First, we need to pass paged parameter and then use the function wp_pagenavi().

We will get the value for paged as follows:

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

So below is our final code.

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

Of course, you can change the HTML tags to fit with your website design.

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

Related Articles

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

21 thoughts on “Display Posts From Specific Category On A WordPress Page

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

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

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

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