A Simple WordPress Custom Type Post for Category

In WordPress, if you want to design a unique theme for posts, you may think about using Custom Post Types. It’s a little complicated to implement custom post type. Sometimes you may just want different theme for a certain category, here I will introduce a simple method.

Create custom theme for category

WordPress has defined the order in which template files are being called for each query type. For category, the template file is called in the following order. It makes easy to create custom theme for category page.

1. category-{slug}.php – If the category’s slug were news, WordPress would look for category-news.php
2. category-{id}.php – If the category’s ID were 6, WordPress would look for category-6.php
3. category.php
4. archive.php
5. index.php

Simplely create a template file category-{id}.php and you will be able to create a unique theme in category-{id}.php for the category. But single post page, WordPress displays the template in the following order.

1. single-{post_type}.php – If the post type were product, WordPress would look for single-product.php.
2. single.php
3. index.php

Unfortunately, there’s no template file related to the single page in above order. What a good thing if WordPress could look for a file like single-{id}.php when the post is queried, in which id – is the category’s ID

Create custom theme for single page

The following code resolves the trick.

function get_custom_template($single_template) {
    foreach( (array) get_the_category() as $cat )
    	if ( file_exists(TEMPLATEPATH . "/single-{$cat->term_id}.php") )
    	   return TEMPLATEPATH . "/single-{$cat->term_id}.php";
add_filter( "single_template", "get_custom_template" ) ;

That’s it! Just create a template file like single-{id}.php. You will be able to create a simple custom post for the category.

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *