Woocommerce Product_cat for current category page

by Joey Ireland   Last Updated September 21, 2018 14:08 PM

i am trying to set a category page up but i want to show the product that are in that category currently. i dont want to make a php template for each category

at the minute it is set as this

<?php $args = array(
    'post_type'      => 'product',
    'posts_per_page' => 10,
    'product_cat'    => 'cat1'
    );
    $loop = new WP_Query( $args );
    ?>

is there a way to make the product_cat to find the products for the page its currently on?

my product code is :

<div class="row products-range search-space">   
                <?php  while ( $loop->have_posts() ) : $loop->the_post();
                    global $product;?>
                    <div class="col-xs-12 col-sm-12 col-md-4">
                        <div class="box">
                            <div class="image">
                                <a href=""><img src="https://via.placeholder.com/600x400" class="img-responsive center-block" /></a>
                            </div>
                            <div class="info">
                                <h3><a href="product-detail.html"><?php the_title(); ?></a></h3>
                                <ul>
                                    <li>13cm Reflex Foam/7cm Top layer</li>
                                    <li>Mains Isolation/Battery backup</li>
                                    <li>5 Year warranty on Mattress and Frame</li>
                                    <li>FREE INSTALLATION and Demonstration</li>
                                </ul>
                                <a class="btn btn-success btn-lg btn-block" href="<?php get_permalink();?>">View</a>
                            </div>  
                        </div>  
                    </div>
                    <?php endwhile;
                                wp_reset_query();?>
                </div>
            </div>
Tags : php woocommerce


Answers 1


Try to get category info using

$category = get_queried_object();


                $cpt_cat = $category->term_id;
                $args = array(
                    'post_type' => 'product',
                    'posts_per_page' => 10,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'product_cat',
                            'field'    => 'term_id',
                            'terms'    => $cpt_cat,
                        ),
                    ),
                );

                // Custom query.
                $query = new WP_Query( $args );

                if ( $query->have_posts() ) {
                    global $wpdb;

                    /* Start the Loop */
                    while ( $query->have_posts() ) : $query->the_post();
                            ?>
                            // Put your HTML stuff here
                            <?php
                        }
                    endwhile;
                    // Restore original post data.
                    wp_reset_postdata();


                } ?>

And use category id/slug in your query for result.

Hope this will help!

Pratik Patel
Pratik Patel
September 21, 2018 13:35 PM

Related Questions


How can add and preview product Frontend

Updated March 26, 2015 12:02 PM


How to add "Reply-to" in Woocommerce

Updated April 04, 2015 15:03 PM


WooCommerce epdq gateway Change order status

Updated April 06, 2015 19:03 PM