最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Get x numbers of a woocommerce category products using Wp_Query

programmeradmin3浏览0评论

Is possible to show some woocommerce products inside a swiper slider? I want to create a slider that will show some "featured" products on the home page. For now I have a cpt that is achieving this, but I don't know how to link them to the products that I will add to woocommerce. So I want to replace the cpt with a query to get an x number of products from woocommerce products and put them into the slider. Any help will be appreciated.

The code I'm using now is this

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

Is possible to show some woocommerce products inside a swiper slider? I want to create a slider that will show some "featured" products on the home page. For now I have a cpt that is achieving this, but I don't know how to link them to the products that I will add to woocommerce. So I want to replace the cpt with a query to get an x number of products from woocommerce products and put them into the slider. Any help will be appreciated.

The code I'm using now is this

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

Share Improve this question asked Jan 16, 2020 at 12:43 sialfasialfa 32910 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Use this query to display woocommerce featured product and replace with yours

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

Then run your slider action html code inside the loop.

<?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(); ?>
发布评论

评论列表(0)

  1. 暂无评论