How to Create a Template for Each Post Category in WordPress
You don’t have to limit yourself to creating a static-page template for your WordPress site. You can use specific templates for the categories you’ve created on your blog and create unique sections for your site.
On the Page below, Portfolio is the name of a category created on the WordPress Dashboard. Instead of using a static page for the display of the portfolio, you can use a category template to handle the display of all posts made in the Portfolio category.
You can create category templates for all categories on your site simply by creating template files with filenames that correspond to the category slug and then uploading those templates to your WordPress
themes directory via SFTP. Here’s the logic behind creating category templates:
- A template that has the filename
category.phpis a catch-all for the display of categories.
- Add a dash and the category slug to the end of the filename to specify a template for an individual category.
- If you don’t have a
category-slug.phpfile, the category display gets defined from the Main Index template (
|If the Category Slug Is …||The Category Template Filename Is …|
You can see three examples of the category template naming requirements.
WordPress makes it possible to pull in very specific types of content on your website through the use of the WP_Query class. If you include WP_Query before The Loop, WordPress lets you specify which category you want to pull information from. If you have a category called WordPress and want to display the last three posts from that category — on your front page, on your sidebar, or somewhere else on your site — you can use this template tag.
WP_Query class accepts several parameters that let you display different types of content, such as posts in specific categories and content from specific pages/posts or dates in your blog archives. The
WP_Query class lets you pass so many variables and parameters that it would take forever to list all the possibilities. Instead, you can visit the WordPress Codex and read about the options available with this tag.
Here are two parameters that you can use with
posts_per_page=X:This parameter tells WordPress how many posts you want to display. If you want to display only three posts, enter posts_per_page=3.
category_name=slug:This parameter tells WordPress that you want to pull posts from the category with a specific slug. If you want to display posts from the WordPress category, enter category_name=wordpress.
Follow these steps to filter posts by category using
- Click the Editor link on the Appearance menu of the Dashboard.
The Edit Themes screen opens.
- Click the template in which you want to display the content.
If you want to display content on a sidebar, for example, choose the Sidebar template:
- Locate the ending </
aside> tag at the bottom of the template for the theme you’re using.
In the Twenty Sixteen theme, the ending </
aside> tag is the last line.Type the following code directly above the ending </
<section id="query" class="widget widget_meta"><h2 class="widget-title">Category Posts</h2>
<?php $query = new WP_Query( array( 'category_name' => 'wordpress' ) ); ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<li><strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong><br />
<?php the_excerpt(); ?> </li>
<?php endwhile; wp_reset_postdata(); ?>
Click the Update File button.
The changes you just made are saved to the
In past versions of WordPress, you used the
query_posts(); tag to pull content from a specific category, but the
WP_Query class is more efficient. Although the
query_posts(); tag provides the same result, it increases the number of calls to the database and also increases page load and server resources, so please don’t use
query_posts(); (no matter what you see written on the Internet!).