WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] show taxonomy term parent title (9 posts)

  1. nadine00
    Member
    Posted 2 years ago #

    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?

    Thanks!

  2. newmediarts
    Member
    Posted 2 years ago #

    This'll work:

    <?php
    $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! :)

  3. nadine00
    Member
    Posted 2 years ago #

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

    P.S. Hi! ^_^

  4. newmediarts
    Member
    Posted 2 years ago #

    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.

  5. nadine00
    Member
    Posted 2 years ago #

    Huzzah! Success. Thank you. ^_^

  6. nadine00
    Member
    Posted 2 years ago #

    ...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.

  7. nadine00
    Member
    Posted 2 years ago #

    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. :)

  8. Henry
    Member
    Posted 2 years ago #

    @newmediarts,

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

  9. Henry
    Member
    Posted 2 years ago #

    @newmediarts

    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.

    Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic