WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Get the term (custom taxonomy) name outside of the loop? (19 posts)

  1. hilj
    Member
    Posted 3 years ago #

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

  2. MichaelH
    Member
    Posted 3 years ago #

    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.

  3. hilj
    Member
    Posted 3 years ago #

    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!

  4. chinmoy
    Member
    Posted 3 years ago #

    use get_terms_by_slug() fuction. check the codex

  5. hilj
    Member
    Posted 3 years ago #

    Where in codex?

    Google doesn't find anything.

  6. MichaelH
    Member
    Posted 3 years ago #

  7. hilj
    Member
    Posted 3 years ago #

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

  8. MichaelH
    Member
    Posted 3 years ago #

    <?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> ';
      }
    }
    ?>
  9. hilj
    Member
    Posted 3 years ago #

    Thanks again Michael!!!!

  10. llaughy
    Member
    Posted 3 years ago #

    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!

  11. MichaelH
    Member
    Posted 3 years ago #

    pulls the custom taxonomy name from the page itself

    echo get_query_var('taxonomy');
  12. llaughy
    Member
    Posted 3 years ago #

    Works beautifully - thanks so much for your help!

  13. Patrick
    Member
    Posted 3 years ago #

    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.

  14. kuhly
    Member
    Posted 3 years ago #

    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

  15. theshae
    Member
    Posted 3 years ago #

    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

  16. sonill
    Member
    Posted 3 years ago #

    same problem here. using wp 3.1.3

  17. keesiemeijer
    moderator
    Posted 3 years ago #

    Use MichaelH's code. The first three arguments of get_term_by are required values. And in Patrick's code he only supplies 2.

    If you need to have links alter MichaelH's code like so:

    $taxonomy = 'concerts';
    $queried_term = get_query_var($taxonomy);
    $terms = get_terms($taxonomy, 'slug='.$queried_term);
    if ($terms) {
      echo '<ul>';
      foreach($terms as $term) {
      	echo '<li><a href="'.get_term_link($term->slug, $taxonomy).'">'.$term->name.'</a></li>';
      }
      echo '</ul>';
    }
  18. sonill
    Member
    Posted 3 years ago #

    thank u very much keesiemeijer

  19. cerysjones
    Member
    Posted 3 years ago #

    This is brilliant, thank you. I had most of it, I was just missing one thing :)

    Out of curiosity, does anyone know how you would get this to output only the slug for the current page's taxonomy?

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags