Blog

You are browsing the archive for How to.

Use the same Category Template for all Child Categories

January 4, 2010

This is a relatively undeveloped code so it hasn’t been tested and I would not suggest using this for production.

How to use the same category-slug.php template for the category and all child categories?

<?php
/**
FORCE CHILD CATEGORIES TO USE PARENT CATEGORY TEMPLATE
Modified code from http://codex.wordpress.org/User:Lorelle/Custom_Category_Template
v0.2
*/
	$jasbt_get_cat = get_category($cat);
	// LET'S FIND OUT IF THIS IS A PARENT CATEGORY PAGE
	if ($jasbt_get_cat->category_parent == 0) {
		$jasbt_get_cat->category_parent = $cat;

	} else {

		// IF PARENT CATEGORY IS "FOO" THEN USE "/CATEGORY-FOO.PHP" THEMPLATE
		$parent_category = get_category($jasbt_get_cat->category_parent);

		// Edit parent category name from 'FOO' to either 'ID', 'category-slug', or 'Category Name'
		if ($parent_category->cat_name == 'FOO') { 

			// Edit the category template from 'category-foo.php' to '/category-ID.php' or '/category-slug.php'
			include(TEMPLATEPATH. '/category-FOO.php');
			exit; // All done.
		}
	}
// IF NOT FORWARDED, CONTINUE WITH THEME ?>
<?php get_header(); ?>

Explanation: If the cat is a parent category, then it does nothing and continues reading either the category.php file or the archive.php (if it cannot find the category.php file then it will look for the archive.php file; then if that one isn’t there, it will use the index.php file). If it has both, then it will first look at the category.php and in which case you’ll want the code there. If the category.php file is not present, then use this code in the archive.php file.

Development: This code is under development and, to me, looks sloppy but it’s a start.

Unique Templates for Categories

January 4, 2010

Recently, I have been developing a theme with special features and figured that I can post some WordPress code goodies here.

How to assign a different category template for a category or a sub-category.

Place the following code before the get_header() hook on the archive/category theme file (i.e. archive.php or category.php file).

In this example, I do not have ‘/category.php’ template because I’m using the ‘/archive.php’ template (click here for WordPress Template Hierarchy diagram ). Instead, I want to assign category templates to certain categories and then continue using my archive template as is.

I want the “Foo” category to have it’s own style separate from other categories and so I created a ‘/category-foo.php’ template file with its own style and added the following code to the first line of my ‘/archives.php’ template file.

<?php
	if (in_category('ID' or 'foo-slug' or 'Foo Name')) {
		include(TEMPLATEPATH . '/category-foo.php');
		exit;
	}
?>

If you have multiple categories that will be assigned to other theme files, then use this code:

<?php
	if (in_category('ID' or 'cat-slug' or 'Cat Name')) {
		include(TEMPLATEPATH . '/cat-template.php');
		exit;
	}
	elseif (in_category('ID' or 'other-cat-slug' or 'Other Cat Nam')) {
		include(TEMPLATEPATH . '/other-cat-temp.php');
		exit;
	}
?>

Unfortunately, this code only works for the parent category or child category that it is specifically assigned and does not automatically work for the child categories of the assigned. I’m currently working on a hack for that.