4

I'm using site tags for a glossary. I would like to display a link to the tagged page if the tag exists, otherwise just display the tag title. Is there a check which would allow me to determine if the tag is has been tagged on posts?

    $tags = get_tags( array( 'hide_empty' => false ) );
    if ($tags) {
      foreach ($tags as $tag) {
        if ($tag->description) {
          echo '<dt style="display:inline; float:left; padding-right:5px;"><strong><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . ' style="text-decoration:none;">' . $tag->name.'</a></strong></dt><dd style="margin-bottom:20px;">' . $tag->description . '</dd>';
        }
      }
    }

(Working) Update from Chip's recommendation

    $tags = get_tags( array( 'hide_empty' => false ) );
    if ($tags) {
      foreach ($tags as $tag) {
        if ($tag->description) {
          echo '<dt style="display:inline; float:left; padding-right:5px;"><strong>';
              if ( 0 < $tag->count ){
                echo '<a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . ' style="text-decoration:none;">' . $tag->name.'</a>';
              } else {
                echo $tag->name;
              }
          echo '</strong></dt><dd style="margin-bottom:20px;">' . $tag->description . '</dd>';
        }
      }
    }

Example end result http://i.imgur.com/aFs6z.png

2
  • One thing I don't understand; you say: "I would like to display a link to the tagged page if the tag exists". Question: where does the tag name come from, if not from an existing tag? Oct 24, 2011 at 19:18
  • The tags comes from the tags and descriptions that were manually generated in order to create a glossary. If the tags are used on a post then I would like them to point to that tag page. Oct 24, 2011 at 19:45

2 Answers 2

5

Try using the has_tag() conditional template tag. e.g., to query for the tag "foobar":

<?php
if ( has_tag( 'foobar' ) ) {
    // The current post has the tag "foobar";
    // do something
} else {
    // The current post DOES NOT have the tag "foobar";
    // do something else
}
?>

If you're inside the Loop, simply call <?php has_tag( $tag ); ?>; if you're outside the Loop, you'll need to pass the post ID: <?php has_tag( $tag, $post ); ?>

So, approximating your code:

$tags = get_tags( array( 'hide_empty' => false ) );
if ( $tags ) {
    foreach ( $tags as $tag ) {
        if ( has_tag( $tag->slug ) ) {
            // Current post has $tag;
            // output the tag link
            echo '<dt style="display:inline; float:left; padding-right:5px;"><strong><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . ' style="text-decoration:none;">' . $tag->name.'</a></strong></dt><dd style="margin-bottom:20px;">' . $tag->description . '</dd>';
        } else {
            // Current post does NOT have the tag;
            // output just the tag name
            echo $tag->name;
        }
    }
}

EDIT

So, another thought: if you're pulling from an arbitrary list of terms, and you want to determine if that term is used as a post tag, then you can try using the term_exists() conditional; e.g. if you want to know if 'foobar' is used as a post tag:

<?php 
if ( term_exists( 'foobar', 'post_tag' ) ) {
    // The term 'foobar' is used as a post tag;
    // do something
}
?>

But I'm still confused about your source of "tags" here.

EDIT 2

So, now we will query based on the tag count being greater than zero (i.e., the tag has been used on at least one post):

    $tags = get_tags( array( 'hide_empty' => false ) );
    if ($tags) {
      foreach ($tags as $tag) {
        if ( 0 < $tag->count ) {
          echo '<dt style="display:inline; float:left; padding-right:5px;"><strong>';
              if ( has_tag( $tag->slug ) ) {
                echo '<a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . ' style="text-decoration:none;">' . $tag->name.'</a>';
              } else {
                echo $tag->name;
              }
          echo '</strong></dt><dd style="margin-bottom:20px;">' . $tag->description . '</dd>';
        }
      }
    }
7
  • Yeah this is outside of the loop. I tried both of your recommendations and they didn't seem to work. Looks like it's simply skipping the if ( has_tag( $tag->slug ) ) { I updated my post You can see it in action here openeye.net/glossary Oct 24, 2011 at 19:03
  • See updated answer. Where are you getting your array of glossary terms? Oct 24, 2011 at 19:21
  • I manually created many of the tags and added the description. We do use many of the terms on the site currently and plan on using all of them eventually when we add the Q&A. Oct 24, 2011 at 19:27
  • "I manually created many of the tags and added the description." - so, these are existing post tags, but you need to know if the count is greater than zero? That's even easier! Oct 24, 2011 at 19:35
  • Correct, I would like to display a link if a posts exists. Oct 24, 2011 at 19:38
1

You can use the count field returned from get_tags to check if it has posts or not, something like this:

$tags = get_tags( array( 'hide_empty' => false ) );
if ($tags) {
    foreach ($tags as $tag) {
        echo '<dt style="display:inline; float:left; padding-right:5px;"><strong>';
        //check tag count
        if($tag->count > 0){
            //its used on posts
            if ($tag->description) {
                echo '<a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . ' style="text-decoration:none;">' . $tag->name.'</a></strong></dt><dd style="margin-bottom:20px;">' . $tag->description . '</dd>';
            }else{
                echo '<a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . ' style="text-decoration:none;">' . $tag->name.'</a></strong></dt>';
            }
        }else{
            //no posts
            echo $tag->name;
            if ($tag->description)
                echo '<dd style="margin-bottom:20px;">' . $tag->description . '</dd>';
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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