如何在woocomerce中获取最近的产品形象


How to get recent product image in woocoomerce

我想在主页中添加最近的wooccommerce产品。

<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 1, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>

此处内容

<?php endwhile; 
wp_reset_query(); ?>

我想在wooccommerce中获得最近的产品特色图片,我写了代码来获得图片。

尝试以下代码:

<ul>
        <?php
            $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
                    <li class="span3">    
                        <a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                            <?php if (has_post_thumbnail( $loop->post->ID )) 
                                    echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); 
                                  else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />'; 
                            ?>                            
                        </a>
                        <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
                    </li><!-- /span3 -->
        <?php endwhile; ?>
        <?php wp_reset_query(); ?>

这可能会对您有所帮助:

$args = array(
        'post_type' => 'product',
        'posts_per_page' => 10,
        'orderby' => 'date',
        'fields' => 'ids',
        'order' => 'DESC');
$newly_arr = query_posts($args);
$all_images = array();
foreach ($newly_arr as $a){
    $post_thumbnail_id = get_post_thumbnail_id($a);
    $all_images[] = wp_get_attachment_url($post_thumbnail_id);
}
echo '<pre>';
print_r($all_images);
die;