Forums

[resolved] Specific template for child categories (3 posts)

  1. nadine00
    Member
    Posted 1 year ago #

    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.

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    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; ?>

  3. nadine00
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic