Pee*_*n87 0 wordpress loops woocommerce
在wordpress front-page.php主题文件中,我循环浏览所有woocoommerce产品并显示特色图片。
<ul class="products">
<?php
// Setup your custom query
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li>
<a href="<?php echo get_permalink( $loop->post->ID ) ?>">
<?php the_post_thumbnail( ); ?>
</a>
</li>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
</ul>
Run Code Online (Sandbox Code Playgroud)
现在我有一些产品是可变产品(不同颜色)。每种颜色都有自己的图像。如何在此循环中显示所有不同的变化图像?我的计划是用这些图像创建一个图像滑块。
小智 5
<?php
$args = array( 'post_type' => 'product' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$product_s = wc_get_product( $loop->post->ID );
if ($product_s->product_type == 'variable') {
$args = array(
'post_parent' => $plan->ID,
'post_type' => 'product_variation',
'numberposts' => -1,
);
$variations = $product_s->get_available_variations();
echo '<pre>';
print_r($variations);
// You may get all images from $variations variable using loop
echo '</pre>';
}
endwhile; wp_reset_query(); // Remember to reset ?>
Run Code Online (Sandbox Code Playgroud)
我还没有测试。但是希望它会起作用。