Search code examples
wordpresstaxonomycustom-taxonomy

Display Based on the parent Term


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


Solution

  • 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