Creating Multiple Single Posts for Different Categories

Assign a specific stylesheet or design for specific category of blog posts in a WordPress blog. There are a lot of ways of doing this, but to be fast and simple, just use a WordPress conditional tag to check which category the blog post was in, and then change the header of the post to the one with the customized stylesheet or design for that particular category.

With only one header template in WordPress Theme, and the conditional tags saying “if this is a single page, show the single page”, something that.

If this is a single page in the X category
show the single page with these styles added.

By default usage, the WordPress Template Hierarchy states that when you click a link to a single post page, WordPress will automatically look for the single.php template file and if it doesn’t find it, it will look for the index.php and return the information in there for displaying a single post.

So we can said, that the condition in the single.php says “if this post belongs to the X category, do something different.”

The If in_category Conditional Tag

The process began by making two back up copies of the single.php page called single1.php and single.2.php.

Inside of the original single.php, delete everything and replace it with this:

$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>

In the most simple terms, the PHP code issues a query that says:
Check the post. If the post is in category ID number 9, display single2.php. If not in category ID number 9, display single1.php.

In the in_category(), we set the category ID number to 9, the one that holds all of my web page design articles and experiments. When the user visits any post within that specific category, the custom stylesheet is used for those blog posts.

This is just the start of what you could do. To showcase different results in different categories, you could create a long list of conditions like this:

$post = $wp_query->post;
if ( in_category('9') ) {
include(TEMPLATEPATH . '/single9.php');
elseif ( in_category('12') ) {
include(TEMPLATEPATH . '/single12.php');
elseif ( in_category('42') {
include(TEMPLATEPATH . '/single42.php');
} else {
include(TEMPLATEPATH . '/single1.php');
} }
?>

You may also like...