如何获得woocoomerce中的最新产品图片

时间:2016-04-12 10:02:43

标签: php wordpress woocommerce

我想在主页上添加最近的woocommerce产品。

<Button ContentTemplate="{StaticResource AllGreenIconTemplate}"/>

内容

<?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;

?>

我想在woocommerce中使用最近的产品特色图片,我写这些代码来获取图像。

2 个答案:

答案 0 :(得分:1)

尝试以下代码:

<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(); ?>

答案 1 :(得分:0)

这可能会对您有所帮助:

$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;