
Every publisher wants to attract more and more customers. For this purpose, the publisher uses different types of plugins to show/hide different features for their users/visitors.
Today we will discuss a beautiful hidden feature of wordpress; how to display total number of posts in anywhere in wordpress. There are two methods to display this feature;
- Using Plugin: a number os plugins are available in wordpress pluging plateform that can help you to display total number of posts. You dont need to use any technical effort like coding in any language as plugin shows clear options in setting. But remember as you use more and more plugings, your site goest down and takes more time to load its features because of heavy plugins. ssecondly proper update of plugings can cause compatibility issue in your setup.
- Using php code: This method is better than using plugin as this option will not take much time to load. To displ;ay total number of posts we need to use three or four lines of php codes. Now lets do it.
By using php code, You can create a template tag or create a shortcode.
Using Template tag
First of all add this code to your theme’s functions.php file. The tag is called post_count and will display the number of posts.
$total = wp_count_posts()->publish;
echo ‘Total Posts: ‘ . $total;
}
This code simply output the total number of posts whenever the template tag wpb_total_posts is called. The next step is to add <?php wpb_total_posts(); ?> in your theme files where you want to display total number of posts.
Using shortcode
If you do not want to use a template tag, then you can create your own shortcode by adding the following code in your function.php page.
$total = wp_count_posts()->publish;
return $total;
}
add_shortcode(‘total_posts’,’post_count’);
This code allows you to use the shortcode [total_posts] to add the information anywhere on your website. Once you have inserted the code of your choice into the functions file, click on the “Save Changes” button to finish.
Customization
Here are something more, You can customize display text accoding to your own views.
function post_count() {
$total = wp_count_posts()->publish;
echo ‘Total Posts: ‘ . $total;
}
The above code will display like;
Total Posts: 123
You can change above red highlisted text according to your own text.
