If you take a look at the template hierarchy in WordPress, you will notice that you have the ability to create custom templates for different category pages by simply adding the category slug or id to the end of your filename. So
category-articles.php
will be called before category.php
if you query the Articles category. But what if you wanted to have a custom template for every post in the Articles category so that it displayed differently from posts in other categories. By default that can’t be done unless you do a little hacking.
Here is an easy way to create custom templates for single posts in WordPress.
First, we need to figure out the category id of the posts we want to customize. Open up your wp-admin and go to the Categories page. Click on the category you want and then take a look in the address bar. You should see something like this:
The 32 at the end is your category id.
Now open up your single.php
file and let’s make a few changes. I will use the single.php
file from the Default theme as an example.
id="post-">Read the rest of this entry »'); ?> 'Pages: ', 'after' => '
', 'next_or_number' => 'number')); ?> Tags: ', ', ', ''); ?>This entry was posted post_date) - (60*120)); echo time_since($entry_datetime); echo ' ago'; */ ?> on at and is filed under . You can follow any responses to this entry through the feed. You can leave a response, or trackback from your own site. Responses are currently closed, but you can trackback from your own site. You can skip to the end and leave a response. Pinging is currently not allowed. Both comments and pings are currently closed.
Sorry, no posts matched your criteria.
To get this little hack to work all you need to do is add:
On line #1 and:
All the way at the bottom.
So now your single.php
file should look like this:
id="post-">Read the rest of this entry »'); ?> 'Pages: ', 'after' => '
', 'next_or_number' => 'number')); ?> Tags: ', ', ', ''); ?>This entry was posted post_date) - (60*120)); echo time_since($entry_datetime); echo ' ago'; */ ?> on at and is filed under . You can follow any responses to this entry through the feed. You can leave a response, or trackback from your own site. Responses are currently closed, but you can trackback from your own site. You can skip to the end and leave a response. Pinging is currently not allowed. Both comments and pings are currently closed.
Sorry, no posts matched your criteria.
Now all you need to do is create a file called single-32.php
and add it to your theme. Code that file however you want your single page posts in category 32 to appear and you are done.
You can also use this hack if you want to create a custom template for a specific post. Just replace in_category()
with is_page()
.