Title: Insert current Tag into a query_posts
Last modified: August 19, 2016

---

# Insert current Tag into a query_posts

 *  Resolved [theApe](https://wordpress.org/support/users/theape/)
 * (@theape)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/)
 * I’m using query_post with multiply loops to create a very irregular layout for
   a theme I’m working on.
 * Does anyone have any pointers on how I can insert the current Tag as stated by
   the URL into a query_post?
 * I’ve been doing what I want with the Categories and Dates, but hit a brick wall
   when it came to the Tags. Here’s an example of what I’m using for Categories.
 *     ```
       <?php  if( is_category() ) {
       foreach((get_the_category()) as $category) { }
       query_posts('showposts=1&category_name=' . $category->category_nicename . '');
       } ?>
       ```
   
 * Any help with code that does something similar for Tags would be appreciated.

Viewing 8 replies - 1 through 8 (of 8 total)

 *  Thread Starter [theApe](https://wordpress.org/support/users/theape/)
 * (@theape)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/#post-1276567)
 * I’m doing this before and outside of the loop, by the way.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/#post-1276574)
 * What about using [is_tag()](http://codex.wordpress.org/Conditional_Tags#A_Tag_Page)
   and [$tags = get_terms(‘post_tag’);](http://codex.wordpress.org/Function_Reference/get_terms)
   and [tag__in](http://codex.wordpress.org/Template_Tags/query_posts#Tag_Parameters)?
 *  Thread Starter [theApe](https://wordpress.org/support/users/theape/)
 * (@theape)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/#post-1276631)
 * Thank you Michael!
 * I had the is_tag() and the tag=, but, I’ve no idea on how to use the get_terms
   with them and I’m not sure if I need an array as I only want to call the one 
   tag at a time. I was also looking at single_tag_title, but my problem is how 
   I pull the output into the parameters of the query_posts.
 * Am I on the right track with the code below.
 *     ```
       <?php  if( is_tag() ) {
       $tags = get_terms('post_tag');
       query_posts('showposts=1&tag=' . $tags . '');
       } ?>
       ```
   
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/#post-1276636)
 * Not sure why you would need to query for a specific tag if you are dealing with
   a tag archive.
 * Related:
    [Tag Templates](http://codex.wordpress.org/Tag_Templates)
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/#post-1276639)
 * Just for reference, this returns the current queried tag.
 *     ```
       <?php
       if (is_tag( )) {
         $tag = get_query_var('tag');
       }
       ?>
       ```
   
 *  Thread Starter [theApe](https://wordpress.org/support/users/theape/)
 * (@theape)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/#post-1276647)
 * Every fifth post in the theme archive has a unique style. So I have a loop which
   calls the first post, then another loop which calls the next four using offset,
   then another one, then another four and so on. Each loop is called by including
   a single php file with its own unique query_post above it to control the output.
 * Thank you for all your help so far, but I’m still getting stuck on how to insert
   the current tag into the query_post parameters.
 * If this calls the current queried tag…
 *     ```
       <?php
       if (is_tag( )) {
         $tag = get_query_var('tag');
       }
       ?>
       ```
   
 * how do I make the current tag replace ‘Something’ in the query?
    `query_posts('
   showposts=1&tag=Something');`
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/#post-1276648)
 *     ```
       <?php
       if (is_tag( )) {
         $tag = get_query_var('tag');
       query_posts('showposts=1&tag='.$tag);
       }
       ?>
       ```
   
 * Also
    [http://www.google.com/search?q=wordpress+styling+each+post+differently](http://www.google.com/search?q=wordpress+styling+each+post+differently)
 *  Thread Starter [theApe](https://wordpress.org/support/users/theape/)
 * (@theape)
 * [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/#post-1276652)
 * Thank you! Thank you! Thank you!
 * You’re a genius!

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Insert current Tag into a query_posts’ is closed to new replies.

## Tags

 * [get_the_category](https://wordpress.org/support/topic-tag/get_the_category/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * 8 replies
 * 2 participants
 * Last reply from: [theApe](https://wordpress.org/support/users/theape/)
 * Last activity: [16 years, 6 months ago](https://wordpress.org/support/topic/insert-current-tag-into-a-query_posts/#post-1276652)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
