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.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
orcategory-slug.php
file, the category display gets defined from the Main Index template (index.php
).
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 |
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.
WP_Query
:
- 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:
sidebar.php
. - 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:
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!).