• Resolved Cathy Tibbles

    (@multitalentedmommy)


    Is there an option to the “include(template…” bit in the code below?

    <?php $id = get_parent_category();
     if ($id == 20) {
     include(TEMPLATEPATH . '/category-'. $id . '.php');}
     else { ?>
    <?php } ?>

    I would like to REDIRECT wp to the custom category-“id” template that I wrote. Instead, it looks like it is including the category-“id” template, and then continuing on with the original one – much like a sidebar or footer! You can see it in action at: /category/recipes If you click on any of the subcategories, you’ll see what I mean. (Except on the misc recipes subcat – NO IDEA why!)

    Am I way off base here?

    Thanks for any help,
    Cathy

Viewing 4 replies - 1 through 4 (of 4 total)
  • The include adds the file to the existing file.

    We’re you looking to change the theme/layout for a particular category ?

    Thread Starter Cathy Tibbles

    (@multitalentedmommy)

    THANK YOU for answering!

    I customized the templatecategory.php and saved as category-20 to be used as the template for the Recipe Category (with an ID of 20). The parent category (category id of 20) is automatically rendered using Category-20.php due to wp’s template hierarchy.

    How can I get the sub-categories of 20 – to use the category-20 template? (Preferably without listing them all separately.)

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    <?php
    if (get_parent_category() == 20) {
    include(TEMPLATEPATH . '/category-20.php');
    return;
    }
    ?>

    Use that code in category.php instead of the code you posted. The return; will cause it to return execution to the calling file, ignoring everything else in the current file.

    Also, there is no get_parent_category so I assume that’s something you made already.

    Thread Starter Cathy Tibbles

    (@multitalentedmommy)

    Thank you thank you thank you Otto!!!! It worked.

    For anyone searching the code for returning sub-categories to the parent category’s template, put this in functions.php:

    //returns a category's parent id if there is one
     function get_parent_category() {
     foreach ((get_the_category()) as $cat) {
     if ($cat->category_parent) return $cat->category_parent;
    else return $cat->cat_ID;}
    }

    And this in the category.php (not the parent category template):

    <?php
    if (get_parent_category() == ID) {
    include(TEMPLATEPATH . '/category-ID.php');
    return;
    }
    ?>

    – replace the “ID” with the id of the parent category. There is probably a nicer way of doing the latter piece of code, but I only have one category to worry about. Thanks to Sivar for his functions piece of code for this.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘is there a “redirect to template path” instead of the “include templatepath”?’ is closed to new replies.