0

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>

1 Answer 1

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!

19
  • where I'm I adding this
    – Jenova1628
    Commented Sep 21, 2018 at 13:37
  • In your texonomy template query Commented Sep 21, 2018 at 13:39
  • I have the category information on the page I want to the pull the products in that category now look at my updated code.
    – Jenova1628
    Commented Sep 21, 2018 at 13:40
  • Checked your code. Is it product archive file right ? Commented Sep 21, 2018 at 13:43
  • I am not too sure I just made a taxonomy-product_cat.php which is where all this is sitting
    – Jenova1628
    Commented Sep 21, 2018 at 13:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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