Custom Category Pages (in_category and get_cat_id)

I’m currently working on a theme that I need to make custom category pages and single pages. I have a plugin almost ready for re-release but instead I went the template route.

One of the first issues I found, the in_category function doesn’t support the category name,

<?php
if (in_category('CATEGORY-NAME')) {
	load_template(TEMPLATEPATH . '/category-special.php');
	}
else {
	get_header();
	load_template(TEMPLATEPATH . '/category-default.php');
}  ?>

and since this client requires the category name (because of multiple development environments having different category ids) a simple get_cat_id retrieved the category ID for in_category. Example,

<?php
if ( in_category(get_cat_id('CAT NAME')) ) {
	load_template(TEMPLATEPATH . '/category-CATNAME-template.php');
	}
else {
	get_header();
	load_template(TEMPLATEPATH . '/category-default.php');
}  ?>

So, the above code will replace the category.php file but make sure to copy the existing code to a new template, in the example above use category-default.php. The rest should be self explanatory.

About the Author, Dan Cameron:

I'm the owner and solution engineer at Sprout Venture, a web solutions company that specializes in web development including WordPress.

I started my first blog in 2003 and transitioned to WordPress in 2004. Since moving to WordPress I've written a few plugins and themes for public consumption. Lately I'm busy engineering/building/coding and have only been able to share a few code snippets.

If you're in need of some web development, web design or custom WordPress plugins and/or themes contact me, I'll be happy to discuss it with you.

Read More »

blog comments powered by Disqus