Filter query_posts by tag slug on “Tag Archive” page (when tag is 2 or more words)

I’m using the following to bring up a tag archive page:

<?php query_posts( "tag=". '' . single_tag_title( '', false ) . '' ); ?>

This works perfectly for all tags of one word only, but any tags of more than one word (eg: “tag one”, slug: “tag-one”) do not display.

Is it possible to query_posts by tag slug, rather than single_tag_title?

Thanks!

1 Answer
1

The single_tag_title() function returns the tag title while you need the tag slug or ID for use in query_posts(). This should get you started:

if ( is_tag() ) {
    $tag       = get_queried_object();
    $tag_title = $tag->name; // Same as single_tag_title()
    $tag_slug  = $tag->slug;
    $tag_id    = $tag->term_id;
}

http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters

Leave a Comment