Ever wanted to display the number of posts your blog has achieved for your blog readers or perhaps just for yourself? Or, what about the number of posts in a specific category?
If so, I’ll show you a quick and easy way to do just that, and we’ll style up the output with a bit of CSS Level 2.1 the standard way.
So, if you guys read JungleJar.com regularly, then by now you should know how I usually give these bits of tutorials / tips / how-tos. I’m going to show you the entire code snippet first, and I’ll explain it line by line when I feel I need to.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php echo '<ul class="postcount">'; foreach (get_categories(array('include'=>421)) as $category) { echo '<li><a href="' . get_bloginfo('wpurl') . '/category/' . $category->category_nicename . '/">' . $category->cat_name . '</a> accounts for ' . $category->count . ' posts </li>'; $num_posts = wp_count_posts( 'post' ); $num_posts = $num_posts->publish; //publish, draft $num_posts = sprintf( __ngettext( '%s post', '%s posts', $num_posts ), number_format_i18n( $num_posts ) ); echo '<li>out of a total of ' . $num_posts . '</li>'; echo '</ul>'; } ?> |
The first line..
1 | echo '<ul class="postcount">'; |
states that we’ll be using an unsorted list with a CSS class of postcount.
In the next line, you’ll notice a foreach PHP loop. You really don’t need to bother yourself with this too much, but you’ll notice a 3 digit number after the (get_categories(array(‘include’=> part. This 3 digit number just happens to be the category ID of the category that I want to display how many posts it has posted to it, or in it, however you want to say it.
1 2 3 4 | echo '<li><a href="' . get_bloginfo('wpurl') . '/category/' . $category->category_nicename . '/">' . $category->cat_name . '</a> accounts for ' . $category->count . ' posts </li>'; |
Okay, the very first line is just declaring a list tag, and as you may have guesed, the bit after that is creating a hyperlink to the category we have supplied the category ID number for. The PHP code also grabs the category nicename, as to not mess up anything later on.
“accounts for” is arbitrary text, and you can put anything you’d like. So, right now there would just be the name of the category and the text “accounts for” unless you decide to change that.
1 2 3 | $num_posts = wp_count_posts( 'post' ); $num_posts = $num_posts->publish; //publish, draft $num_posts = sprintf( __ngettext( '%s post', '%s posts', $num_posts ), number_format_i18n( $num_posts ) ); |
All of this is simply getting the total number of blog posts your website/blog has published and assigns that number to a PHP variable named $num_posts.
And for the last bit..
1 2 3 | echo '<li>out of a total of ' . $num_posts . '</li>'; echo '</ul>'; } ?> |
We declare our list and write up some comparative text which would be “out of a total of”. Feel free to change this as you’d like. Next, we simply display the total number of our blog’s posts.
So, now lets take a look at that CSS unsorted list class named postcount.
1 2 | ul.barpostcount { font-size:11px; margin-top:-2px; color:#000 } ul.barpostcount li { display:inline; padding-right:10px } |
Okay, so, for the unsorted list we have decided to display all text in this list set in a font-size of 11px and also display the text in the color black.
Next are the unsorted list’s lists declarations, and here we are basically saying, “I would like my unsorted list’s items to all remain in the same line (display:inline), and after each time we use the <li> html/xhtml tag, I would like to create 10 pixels worth of space to the right of the previous list tag used.


