I am using WooCommerce with Wordpress. I am trying to return all the items within a category, however, I am returning multiple results depending on how many items I have.
When I only had two tshirtt-shirt pictures, it would display each picture twice. Once I added a third shirt, it would show each shirt 3 times. My code is: <?php
$args = array(
'post_type' => 'product',
'stock' => 1,
'posts_per_page' => 9,
'product_cat' => $pagename,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
foreach ( $loop->posts as $p ) {
$products = new WC_Product($p->ID);
echo "<div class='col-md-4'>";
the_post_thumbnail();
echo "</div>";
wp_reset_query();
}
endwhile;
?>
I have tried moving the wp_reset_query()wp_reset_query()
to multiple places (ie within the foreach loop) however the best I get is one div with the proper photo and then a number of blank divs. When using var_dump()var_dump()
, it looks like it may be the new WP_Query($args)WP_Query($args)
itself.
I was previously using wp_reset_postdata()wp_reset_postdata()
and have switched to wp_reset_query()wp_reset_query()
due to some of the results found on here, but it has not helped.
Thanks in advance.