display the number of posts using a tag in the sidebar?
-
Hello,
I’m trying to use a snippet I found on this post display post count to make a list of tags used (wrapped in a hrefs) the amount they are used and display it in my sidebar.
Here is a link to my site, the Tags section is near the bottom of the sidebar gtsbThe snippet I found that user and moderator Mark / t31os posted on that topic is this:
<?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.' ) '; // Unset the data when it's not needed unset($post_tag); } ?>
I did a minor visual adjustment to the brackets and wrapped it in this:
<div class="sbtags"> <?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.'<span>('.$post_tag->count.')</span>'; // Unset the data when it's not needed unset($post_tag); } ?>
- What would I need to do to get each tag result (not the number) wrapped in a link? (that link I would assume would go to search.php?… ).
- A couple bonuses that would be nice (if it’s not to much reworking of the code) would be to limit the list to a certain amount of tags (perhaps the 15 most used?), and wrap each in an for semantic purposes.
Thanks in advance for any help!
Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
- The topic ‘display the number of posts using a tag in the sidebar?’ is closed to new replies.