I need to calculate some dynamic CSS based on how many top level categories exist. (NOT how many posts are in a category, or how many categories a post belongs to… Nothing about posts at all, just how many top level categories there are)
I am new to PHP, but I couldn’t find any WP functions that would return the number I’m looking for.
Thanks in advance for any help.
As the Codex says, this is the code block to query for only the Top Level categories â the parents. With this, I used the PHP function
count()
.You could do it using get_categories()
Wrap it in a function and return the value instead of echoing it if you need to store it for use in another part of your code.
Edit – I see another version of this was posted already. Either one should work. Mayeenul Islam’s was probably a bit more elegant since he uses the count function (which I should have done… and now have). They’re basically the same thing.