WordPress For Dummies, 8th Edition
Book image
Explore Book Buy On Amazon
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.

WordPress Category template
The Portfolio page, which uses a category template.

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.php is 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.php or category-slug.php file, the category display gets defined from the Main Index template (index.php).
WordPress Category Template Naming Conventions
If the Category Slug Is … The Category Template Filename Is …
portfolio category-portfolio.php
books category-books.php
music-i-like category-music-i-like.php
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.

The 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 WP_Query:

  • 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 WP_Query:
  1. Click the Editor link on the Appearance menu of the Dashboard.

    The Edit Themes screen opens.

  2. 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: sidebar.php.

  3. 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 aside> tag:

    Category Posts

    'wordpress' ) ); ?> have_posts() ) : $query->the_post(); ?>


  4. Click the Update File button.

    The changes you just made are saved to the sidebar.php template.

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

About This Article

This article is from the book:

About the book author:

Lisa Sabin-Wilson is cofounder of WebDevStudios, one of the largest WordPress design and development agencies in the world. She is a regular public speaker at national events on topics such as WordPress, development, design, CSS, and social media.

This article can be found in the category: