Luc
Luc

Reputation: 1825

wordpress - custom homepage with products in carousel

I'm using WooCommerce to build a webshop.

I made a custom homepage. On this homepage, I want to display the products per category in a carousel like structure(3 per view). But how do I get the data(products per category) into my own custom homepage? To be clear, this homepage comes before the product overview(or: 'archive-product.php').

Thanks!

Upvotes: 1

Views: 626

Answers (1)

TheJokersThief
TheJokersThief

Reputation: 138

Modified slightly from source: https://wordpress.stackexchange.com/a/67266/16086

You'll want to use something like this:

<ul class="products">
    <?php
        // Change product_cat to the slug of the category you want
        $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' );
        $loop = new WP_Query( $args );
        while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>

            <h2>Shoes</h2>

                <li class="product">    

                    <a href="<?php echo get_permalink( $loop->post->ID ) ?>" title="<?php echo esc_attr($loop->post->post_title ? $loop->post->post_title : $loop->post->ID); ?>">

                        <?php if (has_post_thumbnail( $loop->post->ID )) echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?>

                        <h3><?php the_title(); ?></h3>

                        <span class="price"><?php echo $product->get_price_html(); ?></span>                    

                    </a>

                    <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>

                </li>

    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
</ul><!--/.products-->

You can just change the product_cat item to the category you want and set posts_per_page to 3.

Upvotes: 1

Related Questions