• Hi,
    I want to use a custom category template for the category (ID=4) and obviously for all its children.

    I tried to use

    <?php if (is_category(4)) : ?>
    <?php load_template(TEMPLATEPATH . '/cat_videopagine.php'); ?>

    <?php endif; ?>`

    and it works just for cat 4 and not for its children….

    how can I do it? Should I add the ID of all children?

Viewing 7 replies - 1 through 7 (of 7 total)
  • try:
    if ((is_category(4)) || ($post->parent == 4))

    actually I think you need to check if is subcategory of 4, so you would put a function in functions.php:

    function is_subcategory (){
        $cat = get_query_var('cat');
        $category = get_category($cat);
        $category->parent;
        return ( $category->parent == '0' ) ? false : true;
    }

    and then do this:
    if (is_category(4)) || (is_subcategory(4))

    Thread Starter jumust

    (@jumust)

    Thanks but it breaks the page.

    I added the function to functions.php and than in category.php I put

    <?php if (is_category(4)) || (is_subcategory(4)) : ?>
    				<?php load_template(TEMPLATEPATH . '/cat_videopagine.php'); ?>
    
    			<?php else : ?>

    Thread Starter jumust

    (@jumust)

    Great it works! Thanks

    But take a look at this highlighted https://pastebin.com/g7tLsywA, it works for parent cat ID 4 and all its subcategory but when I use elseif to make it work for another one (ID 5) it happens that for the parent ID 5 it works but for all its children is still getting the default

    I can’t see a reason why it works only for parent 5.

    Thread Starter jumust

    (@jumust)

    I’m getting crazy, can’t find the reason of this issue, but I figured out one thing trying to play with categories.

    Actually the code works fine with the category 4

    <?php if (is_category_or_sub(4)) : ?> 
    
    				<?php load_template(TEMPLATEPATH . '/cat_videopagine.php'); ?>
    
    			<?php else : ?>

    Even though I don’t use an ELSEIF but just I replace the 4 with 5 or 6 it’s not working for sub-categories of 5 or 6 parents.
    So the issue is related to some categories and not to the code but it’s really strange…
    Hope SOMEONE CAN HELP, here it’s the pastebin of category.php, see line 27 it now works for 4 and its subcategory.
    For example If I replace it with 5 it works only for 5 itself but not for its sub-categories…

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘While creating a custom category template’ is closed to new replies.