Come faccio a creare un Loop custom di prodotti di WooCommerce in WordPress?

Come faccio a creare un Loop custom di prodotti di WooCommerce in WordPress?

Creare un Loop custom di prodotti di WooCommerce non è difficile se si conosce il modo con cui i prodotti vengono gestiti in WordPress.

I prodotti non sono altro che custom post type di tipo product. Quindi:


<?php
	$args = array( 'post_type' => 'product', 'posts_per_page' => -1 );
	$loop = new WP_Query( $args );
	
	if( $loop->have_posts() ):
		while( $loop->have_posts() ):
		    $loop->the_post();
?>
	<div class="my-product">
		<figure class="my-product-image">
			<a href="<?php the_permalink(); ?>">
				<?php the_post_thumbnail(); ?>
			</a>
		</figure>
		<p><a href="http://sito/cart/?add-to-cart=<?php the_ID(); ?>" class="my-add-to-cart-btn"><?php _e( 'Aggiungi al carrello', 'miotema' ); ?></a></p>
	</div>
<?php
	endwhile;
	wp_reset_postdata();
	endif;
?>

Torna su