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
Try using the
has_tag()
conditional template tag. e.g., to query for the tag “foobar”: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:
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: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):
You can use the count field returned from
get_tags
to check if it has posts or not, something like this: