使用WP_QUERY获取WooCommerce类别产品的x个编号

时间:2020-01-16 作者:sialfa

是否可以在swiper滑块内显示一些woocommerce产品?我想创建一个滑块,在主页上显示一些“特色”产品。目前,我有一个cpt正在实现这一目标,但我不知道如何将它们与我将添加到woocommerce的产品联系起来。因此,我想用一个查询来替换cpt,以从woocommerce产品中获得x个数量的产品,并将它们放入滑块中。任何帮助都将不胜感激。

我现在使用的代码是

<div class="row featured-row"
<?php $featured = new WP_Query( [\'post_type\' => \'featured\', \'order\' => \'ASC\', \'posts_per_page\' => 3] ); ?>
<?php $i = 0; ?>
<?php if( $featured->have_posts() ): while( $featured->have_posts() ): $featured->the_post(); ?>
<?php #$class = get_post_meta( get_the_ID(), \'class\', true); ?>
<?php if( $i === 1 ): ?>
  <div class="col-md-6 col-lg-6 d-none d-sm-none d-md-block featured-desc">
    <h2 class="featured-title-right text-right"><?php the_title(); ?></h2>
    <p class="featured-excerpt-right text-right"><?php echo get_the_excerpt(); ?></p>
  </div>
  <div class="col-md-6 col-lg-6 text-center d-none d-sm-none d-md-block featured-img">
    <img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
  </div>
<?php else: ?>
  <div class="col-md-6 col-lg-6 text-center d-none d-sm-none d-md-block featured-img">
    <img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
  </div>
  <div class="col-md-6 col-lg-6 d-none d-sm-none d-md-block featured-desc">
    <h2 class="featured-title"><?php the_title(); ?></h2>
    <p class="featured-excerpt"><?php echo get_the_excerpt(); ?></p>
  </div>
<?php endif;?>
<?php $i++; ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
</div>

1 个回复
最合适的回答,由SO网友:Tejas Gajjar 整理而成

使用此查询显示woocommerce特色产品并替换为您的产品

$meta_query   = WC()->query->get_meta_query();
$meta_query[] = array(
    \'key\'   => \'_featured\',
    \'value\' => \'yes\'
);
$featured = array(
    \'post_type\'   =>  \'product\',
    \'stock\'       =>  1,
    \'showposts\'   =>  6,
    \'orderby\'     =>  \'date\',
    \'order\'       =>  \'DESC\',
    \'meta_query\'  =>  $meta_query
);
然后在循环中运行滑块动作html代码。

<?php $i = 0; ?>
<?php if( $featured->have_posts() ): while( $featured->have_posts() ): $featured->the_post(); ?>
<?php #$class = get_post_meta( get_the_ID(), \'class\', true); ?>
<?php if( $i === 1 ): ?>
  <div class="col-md-6 col-lg-6 d-none d-sm-none d-md-block featured-desc">
    <h2 class="featured-title-right text-right"><?php the_title(); ?></h2>
    <p class="featured-excerpt-right text-right"><?php echo get_the_excerpt(); ?></p>
  </div>
  <div class="col-md-6 col-lg-6 text-center d-none d-sm-none d-md-block featured-img">
    <img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
  </div>
<?php else: ?>
  <div class="col-md-6 col-lg-6 text-center d-none d-sm-none d-md-block featured-img">
    <img class="img-fluid" src="<?php the_post_thumbnail_url(); ?>">
  </div>
  <div class="col-md-6 col-lg-6 d-none d-sm-none d-md-block featured-desc">
    <h2 class="featured-title"><?php the_title(); ?></h2>
    <p class="featured-excerpt"><?php echo get_the_excerpt(); ?></p>
  </div>
<?php endif;?>
<?php $i++; ?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>