0

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

rahul251
  • 68
  • 1
  • 2
  • 13

2 Answers2

1

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
  • Thanks, but i want to check for the parent taxonomy.. Cos i have multiple terms under cars..Something like.. if parent_tax is cars then do widget 1 ..... – rahul251 Jun 09 '14 at 13:46
  • Then simply use `is_tax('cars')` - the first argument is the taxonomy name (cars, motorcycles) and the second argument is the term inside the taxnomoy (cars -> bmw, motorcycles -> ducati). You don't need the second argument if all you need to do is show a cars widget or a motorcycles widget - but they are there if you want to get very specific with a widget or sidebar. – Calvin deClaisse-Walford Jun 09 '14 at 13:55
  • Nevermind, I just saw the structure of your taxonomy in the question! Apologies! – Calvin deClaisse-Walford Jun 09 '14 at 13:56
  • This is what im using if(is_tax( 'custom_categories', 'cars' )).. This works only for cars, but not for the child terms of cars – rahul251 Jun 09 '14 at 13:59
  • Thats ok, but any solution to my question? – rahul251 Jun 09 '14 at 14:00
  • My edit re-approached the problem considering your use of a single custom tax - I haven't done a setup like this, but I believe it will work. – Calvin deClaisse-Walford Jun 09 '14 at 14:14
  • Thanks Calvin!!! I used the parent id $term->parent and gave the condition!! and it works perfectly – rahul251 Jun 10 '14 at 07:24
0

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.
                  }
Jem
  • 744
  • 2
  • 7
  • 25