/** * wpsc_get_term_parents - get all parents of the term * * @param int $id - id of the term * @return array of term objects or empty array if anything went wrong or there were no parents */ function wpsc_get_term_parents($term_id, $taxonomy) { $term = get_term($term_id, $taxonomy); if (empty($term->parent)) { return array(); } $parent = get_term($term->parent, $taxonomy); if (is_wp_error($parent)) { return array(); } $parents = array($parent->term_id); if ($parent->parent && $parent->parent != $parent->term_id && !in_array($parent->parent, $parents)) { $parents = array_merge($parents, wpsc_get_term_parents($parent->term_id, $taxonomy)); } return $parents; }
/** * wpsc print category classes function * places classes for the category including selected state * * please note that "current category" means the category that we are in now, * and not the category that we are printing for * * @param $category_to_print - the category for which we should print classes * @param $echo - whether to echo the result (true) or return (false) */ function wpsc_print_category_classes($category_to_print = false, $echo = true) { global $wp_query, $wpdb; $result = ''; //if we are in wpsc category page then get the current category $curr_cat = false; $term = get_query_var('wpsc_product_category'); if (!$term && get_query_var('taxonomy') == 'wpsc_product_category') { $term = get_query_var('term'); } if ($term) { $curr_cat = get_term_by('slug', $term, 'wpsc_product_category'); } //check if we are in wpsc category page and that we have a term_id of the category to print //this is done here because none of the following matters if we don't have one of those and we can //safely return if (isset($category_to_print['term_id']) && $curr_cat) { //we will need a list of current category parents for the following if statement $curr_cat_parents = wpsc_get_term_parents($curr_cat->term_id, 'wpsc_product_category'); //if current category is the same as the one we are printing - then add wpsc-current-cat class if ($category_to_print['term_id'] == $curr_cat->term_id) { $result = ' wpsc-current-cat '; } elseif (in_array($category_to_print['term_id'], $curr_cat_parents)) { $result = ' wpsc-cat-ancestor '; } } $result = apply_filters('wpsc_print_category_classes', $result, $category_to_print); if (!empty($result)) { if ($echo) { echo $result; } else { return $result; } } }