Support » Themes and Templates » show taxonomy term parent title

  • Resolved nadine00


    How do I modify this:

    <?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); echo $term->parent; ?>

    To show me the the name of the parent taxonomy rather than just an integer?


Viewing 8 replies - 1 through 8 (of 8 total)
  • This’ll work:

    $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    $parent = get_term($term->parent, get_query_var('taxonomy') );
    echo $parent->name;

    It does an additional database query, but it still gives you the desired output without writing any custom SQL.

    Update: I forgot that get_term() requires two parameters to be provided to it, not just the term_id. Updated my code above to reflect this.

    P.S. Hi, Nadine! 🙂

    Hmm. That does not work…odd because I feel it should work.

    P.S. Hi! ^_^

    Oops, I forgot that the get_term() function requires two parameters to be provided to it, not only the term_id. I’ve updated the code above to reflect this.

    Huzzah! Success. Thank you. ^_^

    …ah shoot. It doesn’t work for top level. eg:

    X Lunch
    – Sandwiches
    — Turkey
    — Tofu
    – Soups

    It will return “Sandwiches” if its in “Turkey” but it won’t return “Lunch” If its in “Sandwiches”. …I just need it to return “Lunch”

    Sorry. My bad.

    Oh duh…I can just do this. It’ll work for what I need.

    <?php echo get_post_type( $post ); ?>

    Adventures in not knowing the ins and outs of custom post types yet. Thanks for your responses. 🙂


    Your solution worked perfectly for me. I only needed to display the first parent name. Thank you and +1 to this.


    I just used your solution for a section time in the Platinum SEO plugin. Page title for custom taxonomy terms do not display the parent term by default. If you have lots of sub terms that are the same it leads to duplicate page titles.

    Using your code I was able to insert the parent term into the page title thereby making it unique across my site.


Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘show taxonomy term parent title’ is closed to new replies.