0

I have new taxonomy "car-brand" and I want to show name of parent taxonomy in child taxonomy page.

My hierarchical taxonomy:

bmw
  -series1
    --F20
    --G40
  -series2 
    --F22
    --G42
benz
  -Aclass 
    --W176
    --W177
  -Cclass
    --W203
    --W204

For example, when user is in F20 taxonomy page I want show this with url:

brand: BMW {level 1 taxonomy}
series: series1 {level 2 taxonomy}
0

1 Answer 1

1

You can use get_ancestors() to get an array of IDs of the ancestors of a term in any taxonomies, and in your case, you would use the last 2 items like the example below, where I used:

// Get the current term ID.
$cat_id = get_queried_object_id();

// Get the term's ancestors.
$parents = get_ancestors( $cat_id, 'car-brand' );

// Get the level 1 ancestor term, and display it.
$brand_cat_id = array_pop( $parents );
printf(
    'brand: <a href="%s">%s</a><br />',
    esc_url( get_term_link( $brand_cat_id ) ),
    esc_html( get_term_field( 'name', $brand_cat_id ) )
);

// Get the level 2 ancestor term, and display it.
$series_cat_id = array_pop( $parents );
printf(
    'series: <a href="%s">%s</a>',
    esc_url( get_term_link( $series_cat_id ) ),
    esc_html( get_term_field( 'name', $series_cat_id ) )
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.