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 building a site glossary and trying to display only tags with descriptions. Is there a way to determine if the description is available and only display the tag when it is?

Here is where I am so far.

    $tags = get_tags();
    if (tag_description($tag->term_id)) {
        if ($tags) {
            foreach ($tags as $tag) {
                echo '<dt><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></dt><dd>' . tag_description($tag->term_id) . '</dd>';
            }
        }
    }
share|improve this question

1 Answer 1

up vote 3 down vote accepted

The object returned by get_tags() should have a description property, so you don't need to try to get it again. Just check to be sure $tags->description isn't empty. You should probably also reorganize so that you don't do anything at all unless if($tags). Thus:

// Get ALL the tags!
$tags = get_tags( array( 'hide_empty' => false ) );
if ($tags) {
  foreach ($tags as $tag) {
    if ($tag->description) {
      echo '<dt><a href="' . get_tag_link( $tag->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $tag->name ) . '" ' . '>' . $tag->name.'</a></dt><dd>' . $tag->description . '</dd>';
    }
  }
}
share|improve this answer
    
Worked flawlessly, Thanks Helen. –  Zach Shallbetter Aug 30 '11 at 20:11
    
It's odd I'm finding that it's not updating the list when I add more. Does it only show tag that are attached to posts? If so, how would I display all tags? –  Zach Shallbetter Aug 31 '11 at 17:14
    
Ah yeah, get_tags() excludes empty ones by default. I've updated my answer to get all of them. Untested, though :) –  helenhousandi Aug 31 '11 at 20:45
    
Looks like it's still only getting tags that are connected to posts. Also, how would you sort them by name instead of number of contained posts? –  Zach Shallbetter Aug 31 '11 at 22:04
    
get_tags() is supposed to return tags alphabetically: codex.wordpress.org/Function_Reference/get_tags . Do you have a plugin or code that could be interfering? The above really should work. –  helenhousandi Aug 31 '11 at 22:19

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.