WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] display the number of posts using a tag in the sidebar? (4 posts)

  1. jalacom
    Member
    Posted 2 years ago #

    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 gtsb

    The 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!

  2. jalacom
    Member
    Posted 2 years ago #

    Any body know how to do this? I'm trying and I'm quite sure i'm concatenating the content wrong.

    This is the line I'm trying to edit into a list item and link:
    echo $post_tag->name.' <span>('.$post_tag->count.')</span>';

    I've done this (which of course isn't working):

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

    I obviously don't know what I'm doing. Any help would be appreciated!

  3. jalacom
    Member
    Posted 2 years ago #

    Ok so I got the answer on a different forum.

    <?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 '<li><a href="'. get_tag_link($tag_id).'">'.$post_tag->name.' </a><span>('.$post_tag->count.')</span></li> ';
    		// Unset the data when it's not needed
    		unset($post_tag);
    	}
    	?>

    This creates a list out of my tags, links them and echos out the number of times the tag is used in posts. It works as I wanted, however I've been told that this is overkill and there's a streamlined way built in to the WordPress API. I'm definitely open to going in that direction, just need to find which direction that is.

  4. jalacom
    Member
    Posted 2 years ago #

    For anyone interested i found this is a better way to do it. It's streamlined and returns the 20 most used tags. You can change that to whatever you like. Hope this helps!

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

Topic Closed

This topic has been closed to new replies.

About this Topic