Right, hopefully a nice and simple one… I’m on a category page with the id of 4, I want to get the category object back so I can interogate a few values.
I’ve had a good old look in the WP codex with little success, remember I don’t want to get the categories from a post, I want the category object from the current category.
Many thanks,
Ben 🙂
To get the category object use
get_category
(codex). It’s easy if you know the name, slug or ID, but if you don’t you could useis_category
to check on which category you are and pass the ID toget_category
.Damn, so I did find the answer I was looking for:
Never mind 🙂
$wp_query->get_queried_object()
is a very versatile function. It will return the current category, tag, custom taxonomy term, author, page or post, depending on the page/archive you are viewing. Especially handy if you want to simplify your archive headings.I’d personally get into a habit of calling
get_term
orget_terms
, as the category functions are only wrapper functions that in turn callget_term(s)
anyway.Familiaring yourself with the term functions will make dealing with custom taxonomies a little easier, because you’ll be calling on these functions in such cases.
http://codex.wordpress.org/Function_Reference/get_term
http://codex.wordpress.org/Function_Reference/get_terms
And a function that i don’t see used a great deal, but can be really handy.
http://codex.wordpress.org/Function_Reference/get_term_by
Which provides a means of fetching a term object based on name, slug or ID.
An interesting case wherein one of many category archive pages included in a custom menu returned an empty array for
required me to run through all the different ways to skin a cat (no pun intended).
The final answer ended up being Ben Everard‘s
Thanks!