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?

Django Reinhardt
  • 1,724
  • 5
  • 21
  • 38

3 Answers3

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;
}
Sumit
  • 4,854
  • 2
  • 29
  • 36
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
}
Max Yudin
  • 6,408
  • 2
  • 26
  • 36
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
}
Pieter Goosen
  • 55,488
  • 23
  • 117
  • 211