0

I wish to create different template pages for all Parent Categories and Subcategories. I was hoping to do it using the hierarchy (eg. something like category-parent.php and category-sub.php), but looking at the flow diagram I found, I don't think there's an option:

enter image description here

I could manually add a category-$slug.php for the parent categories (as these shouldn't be changing) and then just use category.php for everything else (ie. the subcategories), but is there a better way?

3

You can do this using custom template and template_include filter. category-parent.php and category-sub.php both are not required. One can be category.php for parent categories and for child categories, you can use custom-category-child.php as category-sub.php is reserved for default hierarchy.

  • First create a template file in theme custom-category-child.php
  • Then add this code to your theme functions.php

Update: As Pieter suggested we can use category_template instead of template_include to save few lines. Additionally we should return current template when our custom template file is missing!

Read inline comments

add_filter('category_template', 'custom_cat_templates');
/**
 * Create custom template for child categroies
 * @param type $template
 * @return type
 */
function custom_cat_templates($template) {
    $category = get_category(get_queried_object_id()); //Get the ID for current queried category
    if ( $category->category_parent > 0 ) { //Check if it child category
        $sub_category_template = locate_template('custom-category-child.php'); //return our custom template
        $template = !empty($sub_category_template) ? $sub_category_template : $template; //return default template if custom templaet missing
    }

    return $template;
}
  • You can just use the category_template filter. Also, your code will break if locate_template('custom-category-child.php') is empty – Pieter Goosen Apr 3 '16 at 6:38
  • Yes, exactly, you should make provision for when custom-category-child.php is missing. – Pieter Goosen Apr 3 '16 at 6:45
  • Updated code. Thank you I was not aware of this filter "{$type}_template" ;) – Sumit Apr 3 '16 at 7:01
  • My pleasure. Enjoy ;-) – Pieter Goosen Apr 3 '16 at 7:02
0

If you do not want to take the scenic route posted by @Sumit (which IMHO is a better solution) and need do do things inside the category template, you can do the following

  • Get the queried object (currently viewed category). We will use $GLOBALS['wp_the_query']->get_queried_object() which is 99.9999% reliable opposed to get_queried_object() or even the suggested get_the_category(). You can read my interesting answer to the subject here

  • Use get_term() to validate the term object (you can also use get_category(), but is short, get_category() is just a wrapper for get_term())

  • From there, we can get the category parent and do what we need to do

We can try the following:

// Get the current category object
$category_object = get_term( $GLOBALS['wp_the_query']->get_queried_object() );

// Run our logic according to the value of $parent
if ( 0 === $category_object->parent ) {
    // Category is top level
} else {
    // Category is a descendant
}
  • You've since fixed it (added ->parent). You were previously testing to see if an object was === 0. Your fix is why I deleted my comment and removed my downvote. – Django Reinhardt Apr 4 '16 at 14:34
  • AH! I see, saw that when I reviewed my code and fixed it immediately – Pieter Goosen Apr 4 '16 at 14:35
0

There is another way if you code the same in the category.php file. Check if the category is a parent category. And if so, then display parent code. Otherwise show the child code. This way you do not need to create multiple files for each category.

$category = get_term(get_queried_object());

if ( $category->category_parent > 0 ) {
    // Is subcategory
} else {
    // Is parent category
}
  • This is not a very reliable method – Pieter Goosen Apr 3 '16 at 6:39
  • @PieterGoosen Could you elaborate a little further? I just about to use this method. – Django Reinhardt Apr 4 '16 at 12:07
  • The code has some serious flaws and will not never work unfortunately – Pieter Goosen Apr 4 '16 at 12:43
  • 1
    Also, it relies on post categories, say a post has many categories, and the queried category is not the first category... Unfortunately, this is totally incorrect – Pieter Goosen Apr 4 '16 at 12:46
  • @PieterGoosen I fixed the code, but my fixes were rejected the community. Very odd. I'm trying again. – Django Reinhardt Apr 4 '16 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.