【问题标题】:fetch products if image is found in woocommerce?如果在 woocommerce 中找到图像,则获取产品?
【发布时间】:2015-04-29 16:14:48
【问题描述】:

我想在 woocommerce 中仅显示图片产品。

下面给出了我的获取查询

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

我怎样才能只获得图像产品?

【问题讨论】:

    标签: woocommerce wordpress


    【解决方案1】:

    你在寻找这样的东西吗?

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

    更新

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

    如果产品有特征图像,则更改 arg 以获取产品。

    【讨论】:

    • 没有@Rohil_PHPBeginner 我想获得产品。有些产品没有产品图片。所以我只需要列出图像可用的产品。指导我
    • @Rohil_PHPBeginner 是正确的。要查询带有特色图片的产品,您必须在 _thumbnail_id 键上运行元查询。不过,Rohil,您是否需要设置 'compare'=&gt;'exists',因为您不是在搜索特定值?
    猜你喜欢
    • 2021-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-10
    • 2014-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多