get_category_count()
September 21st, 2008get_category_count() is a WordPress function to count the posts within a category. It can probably be written in many different ways.
Here is my version of the function and should be placed in functions.php in your theme:
function wt_get_category_count($input = '') {
global $wpdb;
if($input == '')
{
$category = get_the_category();
return $category[0]->category_count;
}
elseif(is_numeric($input))
{
$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=$input";
return $wpdb->get_var($SQL);
}
else
{
$SQL = "SELECT $wpdb->term_taxonomy.count FROM $wpdb->terms, $wpdb->term_taxonomy WHERE $wpdb->terms.term_id=$wpdb->term_taxonomy.term_id AND $wpdb->terms.slug='$input'";
return $wpdb->get_var($SQL);
}
}
It can be called in three different ways…
1. Just call the function within The Loop and it will return the count of the first category within the current post.
<?php echo wt_get_category_count(); ?>
2. Use the function with an ID and it will return the count of the first category within the post ID.
<?php echo wt_get_category_count(1); ?>
3. Use the function with a slug / nicename and it will return the count of the first category within the post slug / nicename.
<?php echo wt_get_category_count('hello-world'); ?>
To get all new functions/template tags, install my plugin WP Extra Template Tags.
It is recommend that use the second one to fetch category count.
is posibile more categori/tags ? … AND $wpdb->terms.slug IN ($input)”;
Hi,
I have the problem with more then 9 articles/category. This function returns only one digit!?
Could anybody help me to solve this problem?
Many thanks in advance!
Ralf
Thanks! It worked just the way I was looking for.
All I can say is thanks!
Hello, I am having trouble getting your syntax to work in my functions.php template.
So, after I place the above code within the functions.php Loop, where do I place the calling script:
At the end of the Loop in the functions.php template?
And, will this give me a count of all posts within each category?
Thanks,
Tumbleweed
Nice work, but there is one thing that does not work. It does not display a number or a parent category, when child categories have posts -> it just shows “0″ posts for parent category.
Great tip. Thanks
Very nice function. Thank you very much.
This is a great function. Thank you. Is it possible to make this function count all posts in the subcategories of the current category?
Hi,
I found this to be of great help!
Thanks
Nathan
Very nice tip, but when a category is empty the function do not return the zero “0″. Is there a way to resolve that ? thanks a lot
Very nice function. Thank you very much. Is it count all posts in sub-cats when you enter a main cat? (parent cat)
I have a website dedicated to Magento and I use this feature on my home page.
However, is it possible to use this function for Link Categories (ie: The number of Blogroll links in a particular cat)?
Thanks.