I would like to show the number of posts on the category page. How is it possible? I tried to find a solution but no luck..
Thank you!
If you are on a category archive you don't need to query all the posts of the category like Christopher Ross suggested and you can't use get_the_category()
like Max Yudin suggested unless you are already in the loop.
What you can do is simply get the current category object using get_queried_object()
which will hold the post count, ex:
$category = get_queried_object();
echo $category->count;
$category = get_the_category();
echo $category[0]->category_count;
If you are on the category page(archive) just use:
$cat= get_the_category();
echo $cat[0]->count;
This works on looping as well. But if you want to show the category count of a specific category anywhere on the site. Use this function:
function wt_get_category_count($catID) {
global $wpdb;
$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->term_taxonomy.term_id=$catID";
return $wpdb->get_var($SQL);
}
echo wt_get_category_count(2);