fetch products if image is found in woocommerce?

2019-09-19 02:51发布

i want to show image only products in woocommerce.

My fetch query is given below

$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 9, 'orderby' =>'date','orderby' => 'rand' );
$loop = new WP_Query( $args );

how can i get the image products only?

1条回答
太酷不给撩
2楼-- · 2019-09-19 03:00

Are you looking for something like this ?

<?php 
    $args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 9, 'orderby' =>'date','orderby' => 'rand' );
    $loop = new WP_Query( $args );
    while ($loop->have_posts()) : $loop->the_post();
        echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
    endwhile;
?>

UPDATED

$args = array( 
                'post_type' => 'product', 
                'stock' => 1, 
                'posts_per_page' => 9, 
                'orderby' =>'date',
                'orderby' => 'rand', 
                'meta_query'=>array(
                    array(
                        'key'=>'_thumbnail_id', 
                        'compare' => 'EXISTS'
                    )
                )
            );

Change arg to get product if it has feature image.

查看更多
登录 后发表回答