$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>posts - Disaply products from category|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

posts - Disaply products from category

matteradmin11PV0评论

I have a working script responsible for displaying woocommerce products on my page which goes something like this:

<?php
$params = array('posts_per_page' => 99, 'post_type' => 'product');
$wc_query = new WP_Query($params);
?>
<div class="list-of-products">
     <?php if ($wc_query->have_posts()) : ?>
     <?php while ($wc_query->have_posts()) :
                $wc_query->the_post(); ?>
     <div class="listed-product">
          <?php the_post_thumbnail(); ?>
          <h4>
               <a href="<?php the_permalink(); ?>">
               <?php the_title(); ?>
               </a>
          </h4>
          <?php the_excerpt(); ?>
          <a class="listed-product-button" href="<?php the_permalink(); ?>">More</a>
     </div>
     <?php endwhile; ?>
     <?php wp_reset_postdata(); ?>
     <?php else:  ?>
     <div>
          <?php _e( 'No products' ); ?>
     </div>
     <?php endif; ?>
</div>

It works fine, but now I wanted to use the same script on another page to only show products from certain category, so I changed the $params line to something like this

$params = array('posts_per_page' => 99, 'post_type' => 'product', 'category_name' => 'category-name');

Where in place of category-name is actuall name of the category I want to display. The problem is it doesn't display any products, instead showing the No products text I set up in case of having 0 products. I tried the same with 'cat' => 43 which is suppoused to be the category id but again no results. Is there something obvious I am missing here?

I have a working script responsible for displaying woocommerce products on my page which goes something like this:

<?php
$params = array('posts_per_page' => 99, 'post_type' => 'product');
$wc_query = new WP_Query($params);
?>
<div class="list-of-products">
     <?php if ($wc_query->have_posts()) : ?>
     <?php while ($wc_query->have_posts()) :
                $wc_query->the_post(); ?>
     <div class="listed-product">
          <?php the_post_thumbnail(); ?>
          <h4>
               <a href="<?php the_permalink(); ?>">
               <?php the_title(); ?>
               </a>
          </h4>
          <?php the_excerpt(); ?>
          <a class="listed-product-button" href="<?php the_permalink(); ?>">More</a>
     </div>
     <?php endwhile; ?>
     <?php wp_reset_postdata(); ?>
     <?php else:  ?>
     <div>
          <?php _e( 'No products' ); ?>
     </div>
     <?php endif; ?>
</div>

It works fine, but now I wanted to use the same script on another page to only show products from certain category, so I changed the $params line to something like this

$params = array('posts_per_page' => 99, 'post_type' => 'product', 'category_name' => 'category-name');

Where in place of category-name is actuall name of the category I want to display. The problem is it doesn't display any products, instead showing the No products text I set up in case of having 0 products. I tried the same with 'cat' => 43 which is suppoused to be the category id but again no results. Is there something obvious I am missing here?

Share Improve this question asked Jan 21, 2019 at 8:58 aMJayaMJay 1273 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you're actually using categories (and not a custom taxonomy) then your category-name needs to be the slug, not the actual name.

If, however, you are using a custom taxononomy (and you'll know if you are) then you should use

 'name-of-your-custom-tax' => 'slug'     

You can also see the below links for details. http://codex.wordpress/Class_Reference/WP_Query#Category_Parameters http://codex.wordpress/Class_Reference/WP_Query#Taxonomy_Parameters

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far