5

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!

3 Answers 3

15

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;
3
$category = get_the_category(); 
echo $category[0]->category_count;

See /Function_Reference/get_the_category

1
  • 2
    get_the_category() is only available when you are in the loop
    – Bainternet
    Feb 7, 2013 at 21:47
1

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.