Show Amount Of Posts, Pages, Categories, Tags, Comments For WordPress Themes
September 27th, 2008 by Frank • WordPress Hacks • 1 Comment
It looks like showing total amount of posts, pages and some other statistical values are very popular right now. I received many inquiries how to show statistical values on your front end.
Following solution only works with WordPress 2.5 or higher. If you need help for a lower version, let me know in the comment field and I'll try to help.
Number of posts
$num_posts = wp_count_posts( 'post' ); $num_posts = $num_posts->publish; //publish, draft
Number of pages
$num_pages = wp_count_posts( 'page' ); $num_pages = $num_pages->publish; //publish
Number of categories
$num_cats = wp_count_terms('category');
Number of Tags
$num_tags = wp_count_terms('post_tag');
Number of comments
$num_comm = get_comment_count(); $num_comm = $num_comm['approved']; //approved, awaiting_moderation, spam, tot // Solution 2 $num_comm2 = wp_count_comments( ); $num_comm2 = $num_comm2->approved; //approved, moderated, spam, total_comments
Output
The above syntax shows simple how to get the values without including HTML. In another example I show you all results in HTML as a list. Everybody can adjust the format to integrate in their design.
<?php $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 ) ); $num_pages = wp_count_posts( 'page' ); $num_pages = $num_pages->publish; //publish $num_pages = sprintf( __ngettext( '%s Page', '%s Pages', $num_pages ), number_format_i18n( $num_pages ) ); $num_cats = wp_count_terms('category'); $num_tags = wp_count_terms('post_tag'); $num_comm = get_comment_count(); $num_comm = $num_comm['approved']; //approved, awaiting_moderation, spam, total_comments $num_comm = sprintf( __ngettext( '%s Categorie', '%s Categories', $num_comm ), number_format_i18n( $num_comm ) ); $num_comm2 = wp_count_comments( ); $num_comm2 = $num_comm2->approved; //approved, moderated, spam, total_comments echo '<ul>'; echo '<li>Posts: ' . $num_posts . '</li>'; echo '<li>Pages: ' . $num_pages . '</li>'; echo '<li>Categories: ' . $num_cats . '</li>'; echo '<li>Tags: ' . $num_tags . '</li>'; echo '<li>Comments: ' . $num_comm . '</li>'; echo '<li>Comments 2: ' . $num_comm2 . '</li>'; echo '</ul>'; ?>
You can adjust the design of these lists. You can use classes or IDs. An example is in article „Total Amount of Comments“ (in german language), where you can see a button with the amount of comments.
Info
- Published in WordPress Hacks
- Tags: comments, counter, page, post, Tags, WordPress
- Comment feed | Trackback URL
- read: 4848 | today: 18
- leave a Comment



great resource, and clear explained
thanks for share it !
adeux