Menu Close

Different Templates For WordPress Category Posts


When I first set up I wanted to have different template designs for my premium themes, free themes, plugins and blog pages. Back then there weren’t any custom post types and custom taxonomies seemed a bit too much, especially since I wanted to keep all the posts within the root folder, so instead I found the code below which allows you to create custom template designs for your single posts based on their categories.

Style Posts From WordPress Categories Differently

Follow the 2 simple steps below so you can create different styles for your single posts for each category.

Step 1: Insert Into Your Functions.php File

Copy and paste the following code into your functions.php file. It creates a new function that assigns a template to your category if a file named single-categoryID.php exits in your theme’s folder.

// add single types
add_filter('single_template', create_function('$t', 'foreach( (array) get_the_category() as $cat ) { if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") ) return TEMPLATEPATH . "/single-{$cat->term_id}.php"; } return $t;' ));

Step 2: Create Your “Templates”

Now you can easily create a template for each of your categories by adding new files to your theme using the following naming structure (simple copy your single.php file, rename and change “categoryid” with your corresponding category id and then style accordingly):

View Source
Posted in WordPress