List only wp category products

Asked

Viewed 373 times

0

Good night, everybody, all right ?

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

Follows 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>
  • I don’t think you posted the full code, please make sure it’s all there.

  • Yes it is only this code, it lists me all the products but I wanted to select only the category, for ex the user was at home clicked on one of the category ex pool table so I should list only the pool tables

1 answer

0

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

The arguments of your query are:

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

You must replace it 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
    );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.