Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

share|improve this question
    
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? –  Chip Bennett Oct 24 '11 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. –  Zach Shallbetter Oct 24 '11 at 19:45

2 Answers 2

up vote 1 down vote accepted

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>';
        }
      }
    }
share|improve this answer
    
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 –  Zach Shallbetter Oct 24 '11 at 19:03
    
See updated answer. Where are you getting your array of glossary terms? –  Chip Bennett Oct 24 '11 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. –  Zach Shallbetter Oct 24 '11 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! –  Chip Bennett Oct 24 '11 at 19:35
    
Correct, I would like to display a link if a posts exists. –  Zach Shallbetter Oct 24 '11 at 19:38

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>';
        }
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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