Reputation: 68
I need to display widget areas based on the parent term..
Eg:-
Cars a. Ford b. Porsche c. Bmw
Bikes a. Honda b. Harley c. Yamaha
my custom taxonomy is custom_categories
If im on the taxonomy page like something.com?custom_categories=ford or custom_categories=bmw or all child categories of Cars "then display the widget area A and if" If im on the taxonomy page something.com?custom_categories=honda or all child categories of Bikes "then display the widget area B "
Eg :- If parent_tax is cars then do widget 1 else if parent_tax is bikes then do widget2
Upvotes: 0
Views: 131
Reputation: 591
To do this, you'll need to get the current term (child taxonomy) of the post you are on, and a parent if it exists:
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$parent = get_term($term->parent, get_query_var('taxonomy') );
//Return an object $term and parent $term
Then look for a specific term by comparing $term->name or seeing if the current term has a parent that matches the name:
if($term->name == 'bikes' || $parent->name == 'bikes')):
// Do widget
else if ($term->name == 'cars' || $parent->name == 'cars')):
// Do other widget
else:
// Do default
Upvotes: 1
Reputation: 769
Sounds like you want parents categories for cars. If you have a model of BMW like an M3 you want to show the widget for the parent category of cars or BMW.
You could use the get_category_parents
function in wordpress. The function isn't well documented if it returns all parents or just the next parent up. So I'm not sure if the function will return just BWM or the full category or cars or both. You might have to nest it in order to go all the way to the top of your categories list.
Regardless once you've got your category returned you can then write an if statement to show a widget or do something.
You'd just do
if ($catParent == 'BWM'){
//call/show widget 1
}else if($catParent == 'Ford'){
//call widget 2 function
}else{
//Call widget 3 or do something completely different.
}
Upvotes: 0