How to display post count on tag page?

In wordpress (ideally, with out the use of a plug-in) on a tag page I would like to show the number of posts that are tagged with the current tag.

Example:
There are 8 posts with the tag “baseball”
when you are on the tag page for baseball it says, “There are 8 posts about baseball”

Read More

It should dynamically know which tag page your on, get the count and print it. I have found several options for statically entering the tag name or ID and returning the count but my attempts to get them to work dynamically have been unsucessful.

This is what I was working with:

$taxonomy = "post_tag"; // can be category, post_tag, or custom taxonomy name


// Using Term Name
$term_name = single_cat_title;
$term = get_term_by('name', $term_name, $taxonomy);


// Fetch the count
echo $term->count;

Any help would be greatly appreciated!

Related posts

Leave a Reply

3 comments

  1. Here is another possible solution from WP Recipes. They were kind enough to post this the day after I got an answer here. 🙂

    <?php 
    $feat_loop = new WP_Query( 'showposts=12&category_name=featured' );
    echo "Query returned ".$feat_loop->post_count." posts.";
    ?>
    

    It has different application but still very useful!

    http://www.wprecipes.com/get-how-many-posts-are-returned-by-a-custom-loop?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Wprecipes+%28WpRecipes.com%3A+Daily+recipes+to+cook+with+WordPress%29