if (is_single()) { $post_categories = get_the_category(); $selected = in_array($cat, $post_categories); // Search for first top level cat of post, and store it foreach ($post_categories as $key => $post_cat) { if ($cat->term_id == $post_cat->term_id) { if (empty($single_root_cat)) { $selected = true; $single_root_cat = $cat; } break; } } } else { // See if the current cat or one of its children is selected $category_children = explode(',', get_category_children_id($cat->cat_ID)); $selected = $is_navigable && (get_query_var('cat') == $cat->term_id || in_array(get_query_var('cat'), $category_children)) && !is_home(); } global $current_cat_color; if ($selected) { $sel_nav_cat_class = 'cat' . $cat->term_id; $current_cat_color = $cat->description; } $cat_class = 'class="' . 'cat' . $cat->term_id; $cat_class .= $selected ? ' active"' : '"'; ?> <li <?php echo $cat_class; ?> >
function get_category_children_id($id) { if (0 == $id) { return ''; } $cat_ids = get_all_category_ids(); foreach ($cat_ids as $cat_id) { if ($cat_id == $id) { continue; } $category = get_category($cat_id); if ($category->category_parent == $id) { $objects .= $category->cat_ID . ','; $objects .= get_category_children_id($category->cat_ID); } } return $objects; }