• Resolved nadine00

    (@nadine00)


    Hello,

    I’m trying to do this kind of a set up:

    If this category is a child of ‘category x’ use ‘template-x’, otherwise use the default template.

    I’ve looked at is_category, and is_ancestor() for reference, but am not sure how to utilize them for this specific purpose (is_cateogry has served me well in the past). Can someone offer some suggestions? The big thing is that I don’t want to manually update an array of categories. Just a simple: if the user makes a category and its the child of X, use X to display it.

    Thank You.

    Nadine.

Viewing 2 replies - 1 through 2 (of 2 total)
  • method 1:
    find parent cat of the category, compare with ID of category x:

    <?php
    $cat_obj = $wp_query->get_queried_object();
    $thisCat = get_category($cat_obj->term_id);
    $parentCat = get_category($thisCat->parent);
    $catx = 32;
    if($parentCat->term_id == $catx) :
        echo 'method 1 - child of x'; //get template x
    endif; ?>

    method 2:
    get direct child categories of category x as IDs into array, use in ‘is_category()’:

    `<?php
    $kitten = array();
    $catx = 32;
    $cats = get_categories(‘parent=’.$catx);
    foreach($cats as $cat) {
    $kitten[] = $cat->term_id; } ;
    if( is_category( $kitten ) ) :
    echo ‘method 2 – child of x’; //get template x
    endif; ?>

    Thread Starter nadine00

    (@nadine00)

    Thank you! That bottom one worked out quite well for me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Specific template for child categories’ is closed to new replies.