WordPress.org

Forums

Get post tags in a widget on a single post page (2 posts)

  1. Sean Hayes
    Member
    Posted 5 years ago #

    How can I get a list of tags relevant to a post on a "single" or "tag" post page for use in a widget on that page?

    I have posts that are tagged and based on what the post is tagged I want the widget to perform a query to display stats and using the tag as a parameter.

    So, if the post is tagged "yellow" and the site is displaying either the posts tagged as "yellow" or a single post tagged "yellow" I want to program the widget to get that tag and use it as a parameter in a database query. The widget will then display the results of that query and the rest of the page will render.

    In some cases I'll want to exclude tags if it means getting the tag I want.

  2. timjh
    Member
    Posted 5 years ago #

    I had the same problem just now. The following worked for me:

    wp_get_object_terms($post->ID, 'post_tag', 'fields=names')
    returns a string array of the tag names associated with the post object $post.

    If you want the tag slugs, it's not so simple. I did this:

    $tags = wp_get_object_terms($post->ID, 'post_tag', 'fields=all');
    foreach ($tags as $tag) {
      $slugs[] = $tag->slug;
    }

    to make $slugs a string array of the slugs.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags