• Hey Community,

    I’m trying to get a tag template to query posts with the same slug as the tag. For example, when I manually enter the script, the code is:

    <?php
    if(is_tag('the-tag'))
    { query_posts('the-tag');
    }
    ?>

    I would like to use single_tag_title() in place of “the-tag,” but I’m very new with PHP and scripting in general. How do I get WordPress to enter the current tag title into the conditional statement?

    Any help is greatly appreciated!
    Fingers crossed…

Viewing 2 replies - 1 through 2 (of 2 total)
  • First, a question: Why do you want to perform a posts query on the ‘current’ tag using query_posts() in a tag template, when that is exactly what will happen by default in a tag template?

    Now for the code:

    <?php
    global $wp_query;
    $tag = $wp_query->get_queried_object();
    
    if( is_tag($tag->name) ) {
        query_posts('tag=' . $tag->name);
    }
    ?>

    Thread Starter startribe

    (@startribe)

    Kaf! First let me say I’m a fan. I love your plugins… especially the_excerpt_reloaded!
    As for your question, first I will explain the overall objective, and this will lead into the answer to the tag query.

    I’m setting up WordPress to manage my projects. I create a master-post to represent the project. Inside of the master-posts are notes/journals on the progress of the project; I was using lists, but I wanted it to be dynamic. So I picked up a plugin that allows me to create a loop within a loop through the post, i.e., the master-post. I’m using tag queries to call these sub-posts into the master-posts. Last, I wanted all latest notes/sub-posts from all projects/master-posts to show up on the project category pages, or home page if desired; most importantly I wanted sub-posts listed on the project category pages to have a reference to the original project page, where all sub-posts are listed with the formal project post, the master-post.

    So… each sub-post is tagged with it’s master-project’s slug, and I thought, “There it is… I will put a conditional statement where if a tag is “example-master-post” then it queries for the “example-master-post” post.

    I couldn’t get your primary script to work, but I think I needed to describe more. Anyhow, I took what you gave me and, with extreme luck/guessing, tweaked it a bit so it shows the slug. And it works! Here is is:

    <?php
    global $wp_query;
    $tag = $wp_query->get_queried_object();
    }
    elseif( is_tag($tag->slug) ) {
        query_posts('name=' . $tag->slug);
    }
    ?>

    Anyhow, thanks Kaf. I look up to your work.

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

The topic ‘Tag Conditional PHP solution’ is closed to new replies.