How to create a linked tag list in my sidebar

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:

Read More
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.

Related posts

Leave a Reply

1 comment

  1. 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.