Forums

Get tags outside the loop (5 posts)

  1. forsgren
    Member
    Posted 3 years ago #

    Hi,

    I'm trying to get the tags for the current post outside the loop (for usage in a widget). I can't find any way to do it and would appreciate any help i can get.

  2. dpcom
    Member
    Posted 3 years ago #

    I'm looking for the exactly same thing. No luck so far :( Any ideas?

  3. mindshare
    Member
    Posted 3 years ago #

  4. timjamz
    Member
    Posted 3 years ago #

    To echo a list of tags from a particular post outside the Loop, you can set $post global, then do a foreach loop to echo the tags as normal.

    For example, I use this loop to display post tags as meta keywords in my header if the page is a single post page.

    <?php
       global $post;
       foreach(get_the_tags($post->ID) as $tag) {
          echo $tag->name . ', ';
       }
    ?>
  5. design_dolphin
    Member
    Posted 3 years ago #

    Cool stuff.

    This is a bit of code I have been working on. In this example the rtrim function removes the last comma of the last word in the list:

    <?php
    $posttags = get_the_tags();
        foreach((array)$posttags as $tag) {
            $show_tags .= $tag->name . ',';
        }{
    $rel_art .= rtrim($show_tags , ',');{
    }
    print $rel_art;
    }
    ?>

    Based on the examples shown here:
    http://www.nathanrice.net/blog/ultimate-guide-to-wordpress-seo-meta-keywords/
    http://phphelper.wordpress.com/php-rtrim-function/

Topic Closed

This topic has been closed to new replies.

About this Topic