I have a custom page template that loops through all custom posts with the post_type of “product_listing” AND the custom taxonomy “product_cat” of “shirts” and returns 4 posts per page (as seen below:)
<?php $loop = new WP_Query( array( 'post_type' => 'product_listing', 'product_cat' => 'shirts', 'posts_per_page' => 4 ) ); ?>
It is the client’s responsibility to manage those categories. I’d like to assign a variable to display in place of “shirts” so that I don’t have to modify the template each time the client adds a new product category (such as shoes, pants, etc.).
I am not a programmer by any means. Does anybody have a snippet of code that would work for this? Or perhaps an article I could read more about assigning dynamic variables in the loop? Thanks!
You could let your client add the product category via a custom field.
This should work if your client adds a custom field value for the key ‘product_cat’.
Apparently I needed to loop the function to set the variable: