List only products in the wp category

0

Good evening guys, how are you?

I'm trying to list only the products of the category when I enter the category page of such a product, what is happening it lists all my products and I can not list only them.

Follow my code:

<div class="wc-products">                       
    <ul class="products columns-3">
        <?php
            $args = array(  'post_type'        => 'product', 
                            'posts_per_page'   => 15, 
                            'taxonomy'     => 'product-category', //woocommerce
                            'orderby'      => 'name',
                            'empty'        => 0
                        );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post();
                global $product;

                $parcela    = get_field("parcela");
                $parcela    = (float)$parcela;
                $parcela    = number_format($parcela, 2, ',', '.');
                $qt_parcela = get_field("qt_parcela");
                $avista     = get_field("a_vista");
                $avista     = (float)$avista;
                $avista     = number_format($avista, 2, ',', '.');
                $price      = get_post_meta( get_the_ID(), '_regular_price', true);
                $formatado  = (float)$price;
                $valor      = number_format($formatado, 2, ',', '.');
                $currency   = get_woocommerce_currency_symbol();

            ?>
            <li class="product type-product status-publish has-post-thumbnail product_cat-mesa-de-sinuca-e-jantar product_cat-mesa-de-sinuca-personalizada first instock sold-individually shipping-taxable purchasable product-type-simple">    

                <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 woocommerce_show_product_sale_flash( $post, $product ); ?>
                    <?php 
                        if (has_post_thumbnail( $loop->post->ID )) 
                            echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');                                               
                        else echo '<img class="attachment-shop_catalog size-shop_catalog wp-post-image" src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="255px" />'; 
                    ?>
                </a>    
                <div class="collection_desc clearfix">
                    <a href="<?php echo get_permalink( $loop->post->ID ) ?>" class="collection_title">
                        <h3><?php the_title(); ?></h3>
                    </a>                                        
                </div>
                <div class='row'> 
                    <div class=''>                                  
                        <div class=' custom-title-product'>
                            <a href=".$link.">
                                <h3 class='custom-title-product'><?php echo $title ?></h3>
                            </a>                        
                        </div>
                <div class='text-center custom-value'>
                    <span>                              
                        <?php echo $currency . $valor ?>
                    </span>
                </div>
                <div class='text-center'> 
                    <span class='custom-inf-parcela '> ou em até <?php echo $qt_parcela ?> x de </span> 
                    <span class='custom-valor-parcela'>  R$ <?php echo $parcela ?> </span> 
                    <span class='custom-inf-parcela '> na entrega </span>
                </div>
                <div class='text-center'>
                    <span class='custom-inf-parcela '> ou apenas </span>
                    <span class='custom-valor-parcela'> R$ <?php echo $avista ?> </span>
                    <span class='custom-inf-parcela '> à vista na entrega <span>
                </div>
            </div>
            </li>                           
        <?php endwhile; ?>
        <?php wp_reset_query(); ?>
    </ul><!--/.products-->
</div>
    
asked by anonymous 02.11.2018 / 04:45

1 answer

0

Simply restrict the search by passing the slug of the current product category. To retrieve the slug, use the get_queried_object () function. This function returns the object of the current page. See below:

The arguments to your query are:

$args = array(  'post_type' => 'product', 
                'posts_per_page'   => 15, 
                'taxonomy'     => 'product-category', //woocommerce
                'orderby'      => 'name',
                'empty'        => 0
       );

You should replace with this:

    $obj = get_queried_object();
    $cat_slug = $obj->slug;
    $args = array(
        'post_type' => 'product',
        'posts_per_page'   => 15,
        'tax_query' => array(
            array(
                'taxonomy' => 'product_cat', //woocommerce
                'field' => 'slug',
                'terms' => array($cat_slug),
                'operator' => 'IN',
            ),
        ),
        'orderby'      => 'name',
        'empty'        => 0
    );
    
18.11.2018 / 23:52