• I have the following category structure on my blog:

    Category Articles
    – Sub Articles
    – Sub Articles
    – Sub Articles
    – Sub Articles

    Category Products
    – Sub Products
    – Sub Products
    – Sub Products
    – Sub Products

    I would like to serve a different single.php for each of the two parent categories (and subsequent child categories). So far I have:

    <?php
    if (in_category('9')) {
    include(TEMPLATEPATH . '/single-articles.php');
    } else {
    include(TEMPLATEPATH . '/single-products.php');
    }
    ?>

    Which would work if I had no child categories beneath articles. I would rather not limit myself to just one article category. Something like this would be great:

    <?php
    if ((in_category('9')) || or child category of 9) {
    include(TEMPLATEPATH . '/single-articles.php');
    } else {
    include(TEMPLATEPATH . '/single-products.php');
    }
    ?>

    I’ve also tried to use the following code by Kaf:

    function is_category_parent($category_id='') {
        global $wp_query;
    
        if (!$wp_query->is_category)
            return false;
    
        $catobj = $wp_query->get_queried_object();
    
        if( ($category_id == $catobj->cat_ID) || ($category_id == $catobj->category_parent) )
            return true;
    
        return false;
    }

    My guess is that it’s not compatible w/2.7.

    Any help would be appreciated. This is the last hurdle to cross 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter tmeyer45458

    (@tmeyer45458)

    The solution:

    <?php
    $in_subcategory = false;
    foreach( (array) get_term_children( 11, 'category' ) as $child_category ) {
    if(in_category($child_category))$in_subcategory = true;
    }
    
    if ( $in_subcategory || in_category( 11 ) ) {
    echo '<span class="fruits">This is about different kinds of fruits</span>';
    } else {
    echo '<span class="bad-cat">Not tasty! Not healthy!</span>';
    }
    ?>

    Figures I find the answer right after asking for help 🙂 Hope this helps someone else!!

    Thread Starter tmeyer45458

    (@tmeyer45458)

    The solution:

    <?php
    $in_subcategory = false;
    foreach( (array) get_term_children( 11, 'category' ) as $child_category ) {
    if(in_category($child_category))$in_subcategory = true;
    }
    
    if ( $in_subcategory || in_category( 11 ) ) {
    echo '<span class="fruits">This is about different kinds of fruits</span>';
    } else {
    echo '<span class="bad-cat">Not tasty! Not healthy!</span>';
    }
    ?>

    Figures I find the answer right after asking for help 🙂 Hope this helps someone else!!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Single.php based on Category/Child Category’ is closed to new replies.