How can i get the category ID in the archive-product.php
template?
I tried to get this information with the $category
var, but this var isn’t accessible in this template file.
1 comment
Comments are closed.
How can i get the category ID in the archive-product.php
template?
I tried to get this information with the $category
var, but this var isn’t accessible in this template file.
Comments are closed.
Normally this should work with
get_queried_object_id()
.Anyway, as I don’t know what Woo exactly does to the query, this can be as well wrong, as the API function references to the object that is currently queried. And this is the object from the last query. So you might also do the following:
Disclaimer: The below written function is not tested and you should
var_dump()
your way through until it works (and then edit this question with your result and delete this lines here).So this is a multi-purpose function, that must be used as plugin (even better: as mu-plugin). It adds itself to the loop/wp_query building process and tries to fetch the ID from the main loop. Then you can call it again by its function name in your template and it should output the ID.
Point is that you’ve to work with
var_dump( $wp_query )
to determine what exactly is set and grab what you need as I don’t know the value names right now straight off my head.