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;

1 Answer
1

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;

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *