WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Insert current Tag into a query_posts (9 posts)

  1. theApe
    Member
    Posted 4 years ago #

    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.

  2. theApe
    Member
    Posted 4 years ago #

    I'm doing this before and outside of the loop, by the way.

  3. MichaelH
    Member
    Posted 4 years ago #

    What about using is_tag() and $tags = get_terms('post_tag'); and tag__in?

  4. theApe
    Member
    Posted 4 years ago #

    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 . '');
    } ?>
  5. MichaelH
    Member
    Posted 4 years ago #

    Not sure why you would need to query for a specific tag if you are dealing with a tag archive.

    Related:
    Tag Templates

  6. MichaelH
    Member
    Posted 4 years ago #

    Just for reference, this returns the current queried tag.

    <?php
    if (is_tag( )) {
      $tag = get_query_var('tag');
    }
    ?>
  7. theApe
    Member
    Posted 4 years ago #

    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');

  8. MichaelH
    Member
    Posted 4 years ago #

    <?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

  9. theApe
    Member
    Posted 4 years ago #

    Thank you! Thank you! Thank you!

    You're a genius!

Topic Closed

This topic has been closed to new replies.

About this Topic