admin管理员组

文章数量:1410717

On a page like taxonomy-product_cat.php - this function lists just the products for the called sub-category - bags:

$args = array( 'post_type' => 'product', 'product_cat' => 'bags')
$loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();

I can hardcode list all the sub-categories and then dynamically list the products. I want to dynamically create the subcategory list with the associated products listed with it - what is the best way to do this? I am thinking that if I make the 'product_cat' => dynamic - is a way to achieve this?

On a page like taxonomy-product_cat.php - this function lists just the products for the called sub-category - bags:

$args = array( 'post_type' => 'product', 'product_cat' => 'bags')
$loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post();

I can hardcode list all the sub-categories and then dynamically list the products. I want to dynamically create the subcategory list with the associated products listed with it - what is the best way to do this? I am thinking that if I make the 'product_cat' => dynamic - is a way to achieve this?

Share Improve this question edited Jan 26, 2020 at 12:19 Jon asked Jan 22, 2020 at 14:52 JonJon 3652 gold badges8 silver badges24 bronze badges 1
  • do you mean the actual category of the category archive page, or a sub-category of that actual category? in what file are you using that code? – Michael Commented Jan 23, 2020 at 20:15
Add a comment  | 

2 Answers 2

Reset to default 1

You shouldn't be using WP_Query at all. The standard loop will automatically display the correct posts.

while ( have_posts() ) : the_post();

endwhile;

You can use get_queried_object(), within that object you can get the slug or id.

本文标签: wp queryWhat code to use in an array to call the current subcategory