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>

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(); ?>
  • Thanks. I was thinking that there was some dedicated template tags to use, but I was wrong. I need also to create a featured category in woocommerce right? – sialfa Jan 16 at 14:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.