I’m building a product archive based on categories and all products are related to a vendor. I have two CPTs (products / vendors) and each product is related to a vendor using Metabox Relationship. This is not a WooCommerce project, it’s just about two simple CPTs related to each other.
What I have done is creating a Archive-Template which outputs all vendors and then queries after all products that belongs to each partner.
What I want to output is only those products that have the current archive-category assigned. So I have for example a category called “shirts”.
On the Archive https://example.com/category/shirts I would then output all vendors and in an inner query i would output all products that have the cat “shirts” and are related to that vendor (very simplified example)
In bricks 1.4 the following code to retrieve the current category slug worked perfectly fine:
$cat = get_category( get_query_var( 'cat' ) );
$slug = $cat->slug; // just for example
In Bricks 1.5.4 the same code produces the following error:
Warning: Undefined property: WP_Error::$slug in /var/www/vhosts/faunt.de/01-sites/faunt/faunt-stage/wp-content/themes/bricks/includes/elements/code.php(159) : eval()'d code on line 7
What is going wrong here? Did Bricks change anything regarding the template architecture under the hood?
Thanks for your fast response. I think my topic was a bit misleading in communication
I don’t really use that code, and i don’t output that code I’ve written in that post. I have a lot of validation inside my code, but while debugging I’ve nailed the problem down to what I’ve written, i can’t access the category information while i am on a cat archive.
So in short:
Just wanted to point out that I cannot get the category slug while I am on a category archive…which should work on every WP category template in a normal theme template .php file… But it doesn’t really work
Hope that is more clear, if not please let me know, maybe i can find another way to tell you what I exactly mean!
In my understanding it should retreive the argument from the main-query, which has the category set on the archive template page?! And also it wouldn’t really explain why the same code works in 1.4 and doesn’t work anymore in 1.5.4
I’m not on my main PC now, where the Project is installed locally… But I think I’ve tried your solution (gonna double check that again tomorrow) as well as other several variations to retreive the category slug which didn’t work either. I’ve turned off all other plugins, which also didn’t have any effect on the result…
You’re right. The function is not just working with the URL. My bad.
BUT… I just noticed: For me both things work. The get_query_var() code you provided in your initial post as well as things like get_the_archive_title().
Hey @aslotta
Many thanks for testing it out!
That’s very interesting, I’m out today and gonna test it in the evening again… I cannot imagine of anything that causes that, specially because I have turned of every plugin to make sure that there are no conflicts.
At least I know now, that it’s not related to the 1.5.4 version, maybe somehow related to some settings in WP or so, gonna double check that. Thanks again!