3

I've managed to do this from a single.php file, inside the loop, but now I need to do it from another page.

I need to find out how many live posts are in category named every story (slug is everystory). I am not inside the loop.

I just need the number, I don't need to output anything except the number. E.g 8

I have tried searching for a way to gather category data just via the category_name (could be name, or could be slug, could even be by category ID) but everything I've found so far refers to using a specific $post var and finding it's category which isn't what I want to do.

Here is the example of what I used on the single post page:

$category = get_the_category();
$counter = $category[0]->category_count;
3
  • Exactly on which page/template Apr 23, 2015 at 8:16
  • @PieterGoosen it's on a page template I have created. The page template outputs a query for some posts. But it shouldn't matter? Is there not a way to call the category data just from the category name/slug/
    – Francesca
    Apr 23, 2015 at 8:21
  • Jip, see my answer, it should work Apr 23, 2015 at 8:24

1 Answer 1

7

I'm a bit unsure of you are actually asking, so apologies if I understood it wrong.

You can use get_category() to get the category object and then just simply echo the $count property value

$cat_count = get_category( 'ID OR ROW OBJECT' );
echo $cat_count->count;

Your Answer

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

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