Zeigen Sie Produkte aus einer bestimmten Produktkategorie in Woocommerce an

Lesezeit: 2 Minuten

Zeigen Sie Produkte aus einer bestimmten Produktkategorie in Woocommerce an
aMJay

Ich habe diesen Code auf meiner WordPress-Site, der Produkte anzeigt, die ich habe:

<?php
$params = array('posts_per_page' => 5, 'post_type' => 'product');
$wc_query = new WP_Query($params);
?>
<ul>
     <?php if ($wc_query->have_posts()) : ?>
     <?php while ($wc_query->have_posts()) :
                $wc_query->the_post(); ?>
     <li>
          <h3>
               <a href="https://stackoverflow.com/questions/50802271/<?php%20the_permalink();%20?>">
               <?php the_title(); ?>
               </a>
          </h3>
          <?php the_post_thumbnail(); ?>
          <?php the_excerpt(); ?>
     </li>
     <?php endwhile; ?>
     <?php wp_reset_postdata(); ?>
     <?php else:  ?>
     <li>
          <?php _e( 'No Products' ); ?>
     </li>
     <?php endif; ?>
</ul>

Es funktioniert gut, aber ich möchte Produkte aus der von mir gewählten Kategorie anzeigen, wie kann ich das tun?

Versuche Folgendes:

<?php

// Here set your product category SLUG (can be multiple coma separated)
$product_categories = array('clothing');

$wc_query = new WP_Query( array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => 5,
    'tax_query' => array( array(
        'taxonomy' => 'product_cat',
        'field'    => 'slug',
        'terms'    => $product_categories,
        'operator' => 'IN',
    ) )
) );
?>
<ul>
     <?php if ($wc_query->have_posts()) : ?>
     <?php while ($wc_query->have_posts()) :
                $wc_query->the_post(); ?>
     <li>
          <h3>
               <a href="https://stackoverflow.com/questions/50802271/<?php%20the_permalink();%20?>">
               <?php the_title(); ?>
               </a>
          </h3>
          <?php the_post_thumbnail(); ?>
          <?php the_excerpt(); ?>
     </li>
     <?php endwhile; ?>
     <?php wp_reset_postdata(); ?>
     <?php else:  ?>
     <li>
          <?php _e( 'No Products' ); ?>
     </li>
     <?php endif; ?>
</ul>

Getestet und funktioniert.

1001230cookie-checkZeigen Sie Produkte aus einer bestimmten Produktkategorie in Woocommerce an

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy