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 looking for a solution to display a tag list that shows how many times the tag has been used in posts as well as link to a list of results. I found a php code snippet you posted on the Wordpress forum here http://wordpress.org/support/topic/how-to-display-the-number-of-posts-under-each-tag?replies=30

<?php
// Select all the post tag IDs
$the_tags = $wpdb->get_col("SELECT term_id
FROM $wpdb->term_taxonomy WHERE taxonomy = 'post_tag'" );

// Loop over each ID, and grab associated data
foreach($the_tags as $tag_id) {
    // Get information on the post tag
    $post_tag = get_term( $tag_id, 'post_tag' );
    // Print the tag name and count (how many posts have this tag)
    echo $post_tag->name.' ( '.$post_tag->count.' )<br />';
    // Unset the data when it's not needed
    unset($post_tag);
}
?>

It's almost perfect for what I need however I'd like to wrap it in a list item and link along with a span over the number. I took the echo line and did this:

echo '<li><a href=\"<?php echo get_tag_link($tag_id); ?>\">'.$post_tag->name.' </a><span>('.$post_tag->count.')</span></li> ';

I gave it my best guess but know it's not right. I know very little about PHP and am not sure if it's just a case of concatenating it correctly or if the get_tag_links part is even correct. Would you be able to help me out? Thanks so much for your time.

share|improve this question
    
There's no reason whatsoever to do a manual SQL query, especially when there are already APIs provided by WordPress that do all the heavy lifting for you –  Tom J Nowell Apr 9 '12 at 18:20
2  
Also why not use the Tags widget? –  Tom J Nowell Apr 9 '12 at 18:21
    
+1 to @Tom J Nowell; 'it's just a case of concatenating' -> echo '<li><a href="'. get_tag_link($tag_id).'">'.$post_tag->name.' </a><span>('.$post_tag->count.')</span></li> '; –  Michael Apr 9 '12 at 18:47
    
Tom,Like I said I know next to nothing about PHP i'm just finding what I can that will work. So i'm not sure what the manual SQL query part would be? If there's a better way and you could point me in the right direction i'm all ears. –  user15031 Apr 10 '12 at 0:49
    

1 Answer 1

up vote 1 down vote accepted

This will output a list of all tags, sorted by the most used tag first. Each tag has the number of times it has been used following the tag in parentheses. The parentheses and number of times used is in a <span>. To remove the parentheses, change <span>(' . $tag->count . ')</span> to <span>' . $tag->count . '</span>

<ul id="tags-list">
<?php
$tags = get_tags( array('orderby' => 'count', 'order' => 'DESC', 'number'=>20) );
foreach ( (array) $tags as $tag ) {
echo '<li><a href="' . get_tag_link ($tag->term_id) . '" rel="tag">' . $tag->name . ' <span>(' . $tag->count . ')</span> </a></li>';
}
?>
</li>
</ul>

This will return the 20 most used tags. Remove , 'number'=>20 to get all tags.

share|improve this answer
    
Travis, Exactly what I needed. Thank you so much. I am getting a parse error on the second line though begins with "$tags = get_tags(...". Any idea what it's missing? Or did I place it wrong? I'm slowly but surely getting the hang of "reading" through the php. But am far from being able to write it. –  joe Apr 11 '12 at 13:18
    
Ok think I found it. There was one extra closing ) at the end. It's working now. Fantastic. –  joe Apr 11 '12 at 13:23
    
Glad it worked for you. I actually needed this exact thing yesterday, so I'm glad I could help. I removed the extra ")" in the code. Give the answer a check mark, since it worked for you. Thanks. –  Travis Pflanz Apr 11 '12 at 13:45

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.