WordPress.org

Ready to get started?Download WordPress

Forums

While creating a custom category template (8 posts)

  1. jumust
    Member
    Posted 2 years ago #

    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?

  2. petra5
    Member
    Posted 2 years ago #

    try:
    if ((is_category(4)) || ($post->parent == 4))

  3. petra5
    Member
    Posted 2 years ago #

    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))

  4. jumust
    Member
    Posted 2 years ago #

    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 : ?>
  5. petra5
    Member
    Posted 2 years ago #

  6. jumust
    Member
    Posted 2 years ago #

    Great it works! Thanks

    But take a look at this highlighted http://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

  7. petra5
    Member
    Posted 2 years ago #

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

  8. jumust
    Member
    Posted 2 years ago #

    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...

Topic Closed

This topic has been closed to new replies.

About this Topic