on my WordPress site, I'm developing a glossary list of novel writing terms, using tags and tags descriptions to draw definitions from. Currently, when any tag has more than 0 posts associated with it, it will link to that tag's archive page, allowing readers to further read up on the topic.

What I would like to achieve, is to have any tag that has exactly 1 post associated with it, to link directly to that post rather than the tag archive. I've been looking around for a solution, but can't seem to find any way to achieve that.

Any help would be greatly appreciated!

For reference, this is the current code:

<!-- Add glossary list in content -->
<?php foreach ( $groups as $group ): ?>
    <div class="glossary-title-link" id ="section-<?php echo str_replace(' ', '-', strtolower($group['name'])) ?>">
    <h4 id ="<?php echo $group['name'] ?>"><?php echo $group['name'] ?></h4>
    <?php foreach ( $group['tags'] as $tag ): ?>
        <li class="glossary-list">

        <!-- If there are any posts for the tag, link to the tag page -->

            if ( $tag['count'] > 0 ) {
                echo '<a class="glossary-link" id="term-';
                echo str_replace(' ', '-', strtolower($tag['name']));
                echo '" href="';
                echo $tag['link'];
                echo '">';
                echo $tag['name'];
                echo '</a></br>';

            } else {
                echo '<div class="glossary-link" id="term-';
                echo str_replace(' ', '-', strtolower($tag['name']));
                echo '">';
                echo $tag['name'];
                echo '</div>';

        <?php echo $tag['description'] ?></li>

    <?php endforeach; ?>
<?php endforeach; ?>

Which leads to this, where 'cliffhangers' is linked and 'denouement' is not:

1 Answer 1


try this ( untested and assuming of course that $tag is the right object tag and $tag['count'] is the number of post with that tag ):

if ( $tag['count'] > 1 ) {
 //first part of your code
else if($tag['count'] == 1){
  $the_query = new WP_Query( 'tag='.$tag['name'] );
  if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
      echo '<a href="';
      echo get_the_permalink();
      echo '">';
      echo $tag['slug'];
      echo '</a></br>';
//// last part of your code
  • Thanks for your quick response! Unfortunately, that doesn't seem to work as intended, with 1-post tags still linking to the full archive, and 0-post tags (i.e. everything covered in the else statement) suddenly disappearing. I've updated my original post to include the full code, if that's any use, with the $group object tag coming from the Tag Groups plugin. The page in its original state can be seen here. Thanks again for your help!
    – Segia
    Commented Jul 23, 2018 at 11:43
  • 1
    have a look at the output of var_dump($tag) (inside the <li>) to see what are the values of it.. Commented Jul 23, 2018 at 12:04
  • That's very useful, I wasn't aware you could do that! Here's what I'm getting for an example 1-post tag: array(7) { ["term_id"]=> int(152) ["link"]=> string(39) "https://nearlycomplete.net/writing/wip/" ["description"]=> string(86) "Work-in-progress. Any novel you're currently pouring your heart, soul and sanity into." ["count"]=> int(1) ["slug"]=> string(3) "wip" ["name"]=> string(3) "WIP" ["tg_font_size"]=> float(12) }
    – Segia
    Commented Jul 23, 2018 at 12:10
  • 1
    so first of all: $the_query = new WP_Query( 'tag='.$tag['slug'] ); for the rest $tag['count'] should be ok to compare with since it's an INT.. Commented Jul 23, 2018 at 12:18
  • That does fix the problem with 0-post tags disappearing, but unfortunately the 1-post tags still link to the archive as before. I've tried to only echo a test string within the else if($tag['count'] == 1) statement, and that didn't show up anywhere either. It seems to somehow not compare with the $tag['count'] properly, even though to the best of my knowledge it should.
    – Segia
    Commented Jul 23, 2018 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.