If this tutorial is not what you were looking for, you still have any questions, suggestions or concerns - feel free to let us know. Please help us to serve you better!

Your Name

Your Email

Your Message (required)

captcha

How to display posts from specific category

This tutorial will show you how to display posts from specific categories in WordPress.

Before you start this tutorial please make sure you learned how to add pages and create page templates in WordPress.

1. Access your theme directory (wp-content/themes/theme###)

2. Create new page, create new page template and assign it to the page.

3. Open the created page template file

To display a posts by specific tag we’ll use the WordPress function "query_posts()"

 

Let’s create a page template that will display all posts from the category "sample". The page template file content would be as follows:

<?php
/*
Template Name: Posts from Category
*/
?>
<?php get_header(); ?>
		<div class="container">
			
				<?php 
				
				query_posts( 'category_name = sample' );
				
				if ( have_posts() ) while ( have_posts() ) : the_post();
				
						echo '<li>';
						
							the_title();
							
						echo '</li>';
						
				 endwhile; 
				
				wp_reset_query(); ?>
					
		</div>
<?php get_footer(); ?>

 

As you can see we inserted the "query_posts()" function that will display posts from the category "sample":

query_posts( 'category_name = sample' );

 

Activated the posts loop that will allow WordPress yto display posts:

				if ( have_posts() ) while ( have_posts() ) : the_post();
				
					...
                    						
				 endwhile; 

 

Added some HTML markup for the posts list.

						echo '<li>';
						
							the_title();
							
						echo '</li>';

 

And closed the "query_posts()" function after the loop.

	wp_reset_query(); ?>

 

You can create a page that will display posts from several categories in several blocks. Just make sure you put full query code into each block.

For example. The page will display posts from 2 categories in 2 blocks:

<?php
/*
Template Name: Posts from Category
*/
?>
<?php get_header(); ?>
		<div class="container">
			<div class="block-1">
			
				<?php 
				
				query_posts( 'category_name = sample1' );
				
				if ( have_posts() ) while ( have_posts() ) : the_post();
				
						echo '<li>';
						
							the_title();
							
						echo '</li>';
						
				 endwhile; 
				
				wp_reset_query(); ?>
					
			</div>
			<div class="block-2">
			
				<?php 
				
				query_posts( 'category_name = sample2' );
				
				if ( have_posts() ) while ( have_posts() ) : the_post();
				
						echo '<li>';
						
							the_title();
							
						echo '</li>';
						
				 endwhile; 
				
				wp_reset_query(); ?>
					
			</div>
		</div>
<?php get_footer(); ?>
How to display posts from specific category, 5.0 out of 5 based on 1 rating