Support » Fixing WordPress » Get the term (custom taxonomy) name outside of the loop?

  • Resolved hilj

    (@hilj)


    How can I get the term name outside of the loop?

    This gets it as a link:

    <?php echo get_the_term_list( $post->ID, 'people', 'People: ', ', ', '' ); ?>

    This without the link:

    $terms_as_text = strip_tags( get_the_term_list( $wp_query->post->ID, 'people', '', ', ', '' ) );
    echo $terms_as_text;

    But not outside loop 🙁

    Here’s some discussion

    Thanks for the help 🙂

Viewing 15 replies - 1 through 15 (of 18 total)
  • Are you asking to get the terms for a particular post or just a list of terms?

    If you want terms for a given post, just replace $post->ID or $wp_query->post->ID with the post ID of the post you desire.

    I’m in a category view and I’d like to do stuff like:

    <?php is concerts ?>
      "Your viewing posts from: <?php echo get_the_term_list( $post->ID, 'concerts', '', ', ', '' ); ?> taxonomy"
    <?php is Exhibitions ?>
      "Your viewing posts from : <?php echo get_the_term_list( $post->ID, 'exhibitions', '', ', ', '' ); ?> taxonomy"
    <?php  start the loop ?>
      Posts
    <?php end loop ?>

    And I can’t get the term outside of the loop.

    Thanks!

    use get_terms_by_slug() fuction. check the codex

    Where in codex?

    Google doesn’t find anything.

    Awwww, I can’t figure it out…

    Ideally I’d love it to work like single_cat_title

    Displays or returns the term name for the current page.

    Thanks so far 🙂

    <?php
    $taxonomy = 'concerts';
    $queried_term = get_query_var($taxonomy);
    $terms = get_terms($taxonomy, 'slug='.$queried_term);
    if ($terms) {
      foreach($terms as $term) {
        echo '<p> This is the Term name ' . $term->name . 'and description '. $term->description . '</p> ';
      }
    }
    ?>

    Thanks again Michael!!!!

    This is exactly what I have been looking for, but I need one more bit of information.

    For the code: $taxonomy = ‘concerts’;

    What do I replace ‘concerts’ with if I am using this for my taxonomy.php page so that it pulls the custom taxonomy name from the page itself? Thanks!

    pulls the custom taxonomy name from the page itself

    echo get_query_var('taxonomy');

    Works beautifully – thanks so much for your help!

    Thanks everyone for your help. I modified this slightly so I could output a list of my taxonomies along with the corresponding slug that was created in the admin panel.

    <?php
    $taxonomy = 'event_type';
    $queried_term = get_term_by( 'slug', get_query_var($taxonomy) );
    $terms = get_terms($taxonomy);
    if ($terms) {
      foreach($terms as $term) {
        echo '<li><a href="' . $term->slug . '">' . $term->name .'</a></li>';
      }
    }
    ?>

    In my case, my custom taxonomy is ‘event_type.’ This code outputs a list of links of the the event types I created, with the anchor set as the slug defined in the admin panel. All you need to do to modify this is replace ‘event-type’ with the name of your custom taxonomy.

    You guys are my new heroes! I can’t tell you how much time I spent on this. Thank you thank you thank you thank you. We go live this weekend and this is what I needed to get the correct page image to show up on my recipepress category pages!

    It’s all down here for me!

    http://new.organicvillefoods.com/recipe-categories/salads/

    Thanks,
    Jeremy

    Patrick, thanks for posting the code.

    Its not quite working for me. It is returning the terms, but the links are not working.

    i get this errror:
    Warning: Missing argument 3 for get_term_by(), called in C:\wamp\www\M\wp-content\themes\jquerymobile\T-new-home.php on line 9 and defined in C:\wamp\www\M\wp-includes\taxonomy.php on line 874

    same problem here. using wp 3.1.3

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘Get the term (custom taxonomy) name outside of the loop?’ is closed to new replies.