A question via @UmarfarukM: Is it possible to apply a new template for a specific category?
There are a couple of different ways to customize templates based on a specific category. If you’re looking to exchange the entire template, we can easily swap which template is loaded by naming our templates correctly. If you just need to change some markup or minor styling, a conditional statement within your generic category template might do the trick. Today we’ll take a look at both, starting with the file naming method.
Just like custom page templates, you can create a custom category template by using the filename category-slug.php, where “slug” is, of course, the slug of the category you’re trying to target. So if you wanted to make a custom template for the category “Movies”, you’d create category-movies.php. To jumpstart the file, you might want to copy category.php (or archive.php, depending on your theme).
You can also target by category ID, meaning if your “Movies” category had an ID of 18, you’d name your custom template category-18.php. You can learn more about which category template will be loaded over in the WordPress Codex.
If the above file-naming information doesn’t sound familiar, I’ll take this time to urge you to get comfortable with the Template Hierarchy. In every situation, WordPress has rules that determine which template files are loaded. Knowing which file will be loaded when your site renders is huge advantage when creating a custom theme. If you’re a visual learner, check out the Template Hierarchy flow chart.
If your changes aren’t extensive enough to warrant swapping out the entire template, consider using a conditional tag inside your normal category or archive template.
Using the following functions, you can easily check when a specific category is being displayed. Take a peek then join me down below for an explanation:
<?php is_category( '9' ) // check by category ID is_category( 'Stinky Cheeses' ) // check by category name is_category( 'blue-cheese' ) // check by category slug is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) ) // mix and match!
Using any of the above functions will return true or false and let our templates know which category is being displayed. Simply use the functions inside an if/else statement and you're on your way!
For instance, if you wanted to do something like not display the sidebar when viewing the "Art" category, you could wrap the_sidebar() in something like the following:
<?php
if (is_category( 'quotes' )) {
// do nothing
} else {
the_sidebar();
}
The above snippet, which you'd place in category.php or archive.php, first tests to see if we're in the category "Art", then takes the appropriate action. If the function returns true, we do not render the sidebar. If it returns false, we show the sidebar as we normally would.
Wrapping up
Using the above methods should be enough to handle just about anything, especially if you go the route of swapping out the file based on slug. Styling your category templates in different ways is a great method for creating 'sections' or 'channels' in your site that have a unique look and feel.
Here is your to-do list:
- master the Template Hierarchy
- learn some conditional tags