Reputation: 259
I have 3 levels of product categories on my site:
Top Level (who has child categories but no parent category) Mid Level (who has both parent category and child categories) End Level (who has parent category but no child categories)
I want to be able to check if a category has parent category and if it has child categories, in order to decide if echo a specific object on the category page or not.
How do I do that?
Tnx Ahead!
Upvotes: 4
Views: 7251
Reputation: 685
Okay, Try the below,
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); // get current term
$parent = get_term($term->parent, get_query_var('taxonomy') ); // get parent term
$children = get_term_children($term->term_id, get_query_var('taxonomy')); // get children
if(($parent->term_id!="" && sizeof($children)>0)) {
// has parent and child
}elseif(($parent->term_id!="") && (sizeof($children)==0)) {
// has parent, no child
}elseif(($parent->term_id=="") && (sizeof($children)>0)) {
// no parent, has child
}
Upvotes: 4
Reputation: 685
You either can detect the parent or child by using this function "cat_is_ancestor_of()" or else you can use "get_ancestors()".
Hope this helps to fix your need.
Upvotes: 0