如何使用WooCommerce插件显示特定于类别的产品?
-
-
你能告诉我在哪里添加此代码吗can u please tell me where to add this code if you please
- 0
- 2017-01-20
- Alaa M. Jaddou
-
3 个回答
- 投票数
-
- 2012-10-05
您需要为此创建一个新循环.这是我用来在首页上显示特定类别产品的代码:
<ul class="products"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <h2>Shoes</h2> <li class="product"> <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 src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?> <h3><?php the_title(); ?></h3> <span class="price"><?php echo $product->get_price_html(); ?></span> </a> <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?> </li> <?php endwhile; ?> <?php wp_reset_query(); ?> </ul><!--/.products-->
You need to create a new loop for that. Here's the code I use for displaying products from a specific category on the home page:
<ul class="products"> <?php $args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'shoes', 'orderby' => 'rand' ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?> <h2>Shoes</h2> <li class="product"> <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 src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="300px" height="300px" />'; ?> <h3><?php the_title(); ?></h3> <span class="price"><?php echo $product->get_price_html(); ?></span> </a> <?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?> </li> <?php endwhile; ?> <?php wp_reset_query(); ?> </ul><!--/.products-->
-
@Vantong然后将此答案标记为**答案:**单击左侧的复选标记.@VanTong Then mark this answer as **the answer:** click the checkmark on the left side.
- 1
- 2012-10-07
- fuxia
-
嗨,Dwaser,我想问更多.如果在存档页面中,我要显示所有新产品.当我选择特定类别时,它将显示该类别的产品.示例我有2个类别Category A和Category B,当我选择A时,它将显示A的乘积,而选择B则将显示B的乘积.请告诉我循环.谢谢.Hi Dwaser,I want ask more. If in archive page, I want show all new product. and When I choose specific Category It will show product of that category. Example I have 2 category Category A and Category B,When I choose A, it will show product of A, and choose B it will show product of B. Please tell me loops. thanks.
- 0
- 2012-10-26
- Van Tong
-
热门节目消息,如果没有产品?hot show message if there is no products?
- 0
- 2014-02-05
- Muhammad Bilal
-
@dwaser嗨,deosproduct_cat参数接受一个int类别ID,即:40而不是'shoes'吗?如果没有,如何使其接受一个int类别ID?谢谢@dwaser Hi, deos the product_cat argument accept an int category id, ie: 40 instead of 'shoes' ? if not, how to make it accept an int category id ? thanks
- 0
- 2014-05-06
- Malloc
-
@dwaser.看起来现在不推荐使用woocommerce_placeholder_img_src()函数.get_image()是否等效?http://docs.woothemes.com/wc-apidocs/source-class-WC_Product.html#1473-1489.此外,为什么还要先检查帖子缩略图?@dwaser. looks like that function woocommerce_placeholder_img_src() has been deprecated now. Would get_image() be the equivalent? http://docs.woothemes.com/wc-apidocs/source-class-WC_Product.html#1473-1489. Also, why do you check for a post thumbnail first?
- 0
- 2015-06-09
- codecowboy
-
不好意思,但是在哪里添加此代码?我是wordpress的新手,但截止日期很近.@万通exicuse me guys, but where to add this code ? i'm a newpie in wordpress but i have a very close deadline. @VanTong
- 1
- 2017-01-20
- Alaa M. Jaddou
-
我也想知道这段代码的上下文.放在哪里?I am also wondering about the context of this code. Where to put it?
- 0
- 2018-02-15
- Primož Kralj
-
迟到了1年,但是您应该将其放入模板中.wp-content/yourtheme/template-name.php1 year late, but you should put this in your template. wp-content/yourtheme/template-name.php
- 0
- 2019-10-16
- Bwrites
-
- 2014-03-14
还有另一种方法:
您可以将"商店"页面分配为默认主页.现在所有产品将显示在主页上.我的意思是
index.php
.There is another way:
You can assign the "Shop" page as default home page. Now all the products will displayed to the home page. I mean
index.php
. -
- 2013-09-17
尝试
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
Try this
<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
-
您能否详细说明这将如何以及为何解决OP的问题?Can you elaborate on how and why this would solve the OP's problem?
- 4
- 2013-09-17
- Johannes Pille
我正在使用插件Woocommerce在线购物.
我有很多类别,例如
shoes
,clothes
等.如何显示特定类别的产品?我在示例页面上看到了这样的产品循环,但我只想在我的
index.php
中显示特定类别的产品.