• Resolved David Gard

    (@duck_boy)


    Hey all,

    I’m trying to find out if a category ID that is selected from $wp_query->query_vars is a child of another Category, but not sure how. Could anybody help me out at all?

    Here is what I am trying to achieve –

    if($wp_query->query_vars['cat'] == {child of Category 51}){
        $activeClass = 'services';
    }

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • From some notes this example you might use:

    <?php
    // in category template, test if queried category is child descendant of another category
    if ( is_category() ) {
      $parent = 8;
      $categories = get_categories('include='.get_query_var('cat'));
      //echo "<pre>"; print_r($categories); echo "</pre>";
      if ( $categories[0]->category_parent == $parent ) {
        echo 'category ' . $categories[0]->name . ' is a child of category ' . $parent;
      }
    }
    ?>

    Thread Starter David Gard

    (@duck_boy)

    Thanks Michael, that helped me sort.

    $categories = get_categories('include='.get_query_var('cat'));
    
    elseif($wp_query->query_vars['cat'] == 51 || $categories[0]->category_parent == 51){
        $activeClass = 'servicesBus';
        $cat = $wp_query->query_vars['cat'];
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Is Category child of a certain other Category’ is closed to new replies.