WordPress.org

Support

Support » How-To and Troubleshooting » How to use a custom category template for all sub categories of a particular cat

How to use a custom category template for all sub categories of a particular cat

  • I’d like to use a custom category template for a section of my website called Destination Guides. Each destination guide is a sub category. All of the destination guide sub categories come under the Destination Guides category.

    I’d like make all of the sub categories of the Destination Guides category use a custom category template.

    I can’t find a way of doing this using the standard functionality. It would be really useful for my website.

    I’d really appreciate any help. Thanks.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator keesiemeijer

    @keesiemeijer

    Create a template file for the subcategories of the destination guide parent category and call it category-destination-guide-sub.php and put this in your functions.php:

    function myTemplateSelect() {
        if (is_category() && !is_feed()) {
            if (cat_is_ancestor_of(3, get_query_var('cat'))) {
                load_template(TEMPLATEPATH . '/category-destination-guide-sub.php');
                exit;
            }
        }
    }
    
    add_action('template_redirect', 'myTemplateSelect');

    Change the category ID (3) in cat_is_ancestor_of(3, get_query_var('cat')) to the correct parent category ID. Now all sub-categories of that parent category will use the category-destination-guide-sub.php template file.

    Thank you so much keesiemeijer.

    I added the code and edited the ID. However I’m now getting a 500 internal server error only on those specific sub categories.

    This is what I added to the functions.php file:

    function myTemplateSelect() {
        if (is_category() && !is_feed()) {
            if (cat_is_ancestor_of(102, get_query_var('cat'))) {
                load_template(TEMPLATEPATH . '/category-destination-guide.php');
                exit;
            }
        }
    }
    
    add_action('template_redirect', 'myTemplateSelect');

    I’ve already created the custom category template so I have a slightly different file name and I edited the parent category ID.

    What do you think has gone wrong?

    Many thanks,
    Chris

    Moderator keesiemeijer

    @keesiemeijer

    I think you need to name the file something different than an existing category slug (“destination-guide”). There may be a conflict because WordPress is also trying to use that file for that category: http://codex.wordpress.org/Template_Hierarchy#Category_display

    Thanks keesiemeijer. I tried renaming the file and the reference in functions.php but unfortunately the sub categories are still loading internal server errors.

    Any thoughts on what else it could be?

    Thanks,
    Chris

    Moderator keesiemeijer

    @keesiemeijer

    Are you using a child theme?

    Try changing this:

    load_template(TEMPLATEPATH . '/category-destination-guide.php');

    to this:

    load_template(get_template_directory() . '/category-first-level-sub.php');

    I tried

    function myTemplateSelect() {
        if (is_category() && !is_feed()) {
            if (cat_is_ancestor_of(102, get_query_var('cat'))) {
                load_template(get_template_directory() . '/category-first-level-sub.php');
                exit;
            }
        }
    }
    
    add_action('template_redirect', 'myTemplateSelect');

    and I also tried

    function myTemplateSelect() {
        if (is_category() && !is_feed()) {
            if (cat_is_ancestor_of(102, get_query_var('cat'))) {
                load_template(get_template_directory() . '/category-destination-guide-subcats.php');
                exit;
            }
        }
    }
    
    add_action('template_redirect', 'myTemplateSelect');

    Unfortunately neither of those worked.

    The theme I am using is a copy of the twenty-eleven theme, which has been renamed — I hope that gives you more idea.

    Thank you again for your help keesiemeijer.

    Moderator keesiemeijer

    @keesiemeijer

    I don’t know why this is not working for you. Maybe try setting WP_DEBUG to true in your wp-config.php to see if any errors show up.
    http://codex.wordpress.org/Debugging_in_WordPress

    Alternatively you can use this to get the different templates in your theme’s category.php:

    <?php
    if (cat_is_ancestor_of(3, get_query_var('cat'))) {
      // category-first-level-sub.php
      get_template_part( 'category-first-level-sub' );
    } else {
      // normal-category-template.php
      get_template_part( 'normal-category-template');
    }
    ?>

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to use a custom category template for all sub categories of a particular cat’ is closed to new replies.
Skip to toolbar