WooCommerce-如何在产品上方显示产品类别?
-
-
为什么这个问题不在主题范围内,XD .....Why this question is off topic, XD.....
- 0
- 2016-06-07
- Brethlosze
-
2 个回答
- 投票数
-
- 2013-03-10
产品标题以优先级5添加到
woocommerce_single_product_summary
挂钩中,因此您希望将其添加到优先级较低(较早)的同一个挂钩中.您可以将其添加到主题的functions.php文件中.我想当您说"类别"时,您指的是WooCommerce产品类别,因此这应该返回第一个类别并在产品标题之前打印它:function wpa89819_wc_single_product(){ $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' ); if ( $product_cats && ! is_wp_error ( $product_cats ) ){ $single_cat = array_shift( $product_cats ); ?> <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2> <?php } } add_action( 'woocommerce_single_product_summary', 'wpa89819_wc_single_product', 2 );
The product title is added to the
woocommerce_single_product_summary
hook at priority 5, so you'll want to add to the same hook with a lower (earlier) priority. You'd add this to your theme's functions.php file. I presume when you say "category" you mean the WooCommerce product category, so this should return the first one and print it before the product title:function wpa89819_wc_single_product(){ $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' ); if ( $product_cats && ! is_wp_error ( $product_cats ) ){ $single_cat = array_shift( $product_cats ); ?> <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2> <?php } } add_action( 'woocommerce_single_product_summary', 'wpa89819_wc_single_product', 2 );
-
非常感谢!您的解决方案效果很好,是的,您猜对了,我的意思是WooCommerce产品类别,而不是wordpress页面类别.Thanks so much! your solution worked great and yes, you guessed right that I meant the WooCommerce product category, not the wordpress page category.
- 0
- 2013-03-18
- user28546
-
大!如果它可以解决您的问题,您可以选择它作为答案吗?Great! If it worked and solved your question can you select it as the answer?
- 0
- 2013-03-18
- helgatheviking
-
嗨,helgatheviking,再次感谢您的帮助.抱歉,我是新手,我看不到如何将其标记为已回答?!?hi helgatheviking, Thanks again for your help. sorry I'm a newbie and i can't see how to mark this as answered?!?
- 0
- 2013-03-26
- user28546
-
所有答案旁边应有一个复选标记.当您单击它时,它将变为绿色,表示这是正确的答案.选择答案可以提高您的百分比,这样人们将来更有可能为您提供帮助.请阅读[FAQ](http://wordpress.stackexchange.com/faq)的"我怎么问问题"部分There should be a check mark next to all the answers. When you click on it it will turn green to signify that this is the correct answer. Choosing answers keeps your percentage up so people will be more likely to help you in the future. Please read the "how do I ask questions" part of the [FAQ](http://wordpress.stackexchange.com/faq)
- 0
- 2013-03-27
- helgatheviking
-
感谢您的解决方案.但是我有一个产品类别,在一个子产品类别中.当我在上面添加代码时,仅打印子产品类别.如何打印此树的第一个父级?Thanks for this solution. But I have a product category and inside a sub product category. When I add the code above only the sub product category is printed. How can I print the first parent of this tree?
- 0
- 2015-06-20
- jpcmf80
-
- 2013-03-10
您需要get_categories函数,该函数带有以下参数:
get_categories( $separator, $before, $after )
因此以简单的形式(假设每个产品仅一个类别),只需添加
<?php echo $product->get_categories(); ?>
到您的content-product.php(第53行). 如果您有更多类别,则可以在单个产品页面上进行操作:<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
you need the get_categories function, which takes the following arguments:
get_categories( $separator, $before, $after )
so in it's simples form (assuming only one category per product), just add
<?php echo $product->get_categories(); ?>
to your content-product.php (line 53). If you have more categories, you can do it like on the single product page:<?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', sizeof( get_the_terms( $post->ID, 'product_cat' ) ), 'woocommerce' ) . ' ', '.</span>' ); ?>
-
请注意,如果要在缩略图上方显示标题,则应将其放在第43行,但这会使事情变得复杂,因为任何" sale"标签等都可以保留在原处(而不是随缩略图一起移动).note that if you want to display the title above the thumbnail, you should put it in line 43, but this complicates things as any 'sale' tags etc. could stay in place (instead of moving with the thumbnail).
- 0
- 2013-03-10
- Ewout
-
我不得不使用$ _product->get_categories(在$ _product中带有下划线),它的效果很好.谢谢!I had to use $_product->get_categories (with the underscore in $_product) and it worked great.Thanks!
- 0
- 2017-02-09
- Peanuts
我是修改wooCommerce的新手. 我希望有人在尝试对插件进行较小更改以适应客户请求时遇到的问题可以帮助我.
我正在尝试修改WooCommerce以显示每个产品的类别名称. 我希望类别名称显示在主商店页面的产品图片上方. (一次在屏幕上显示许多产品的视图)
我已经四处搜寻,但似乎找不到如何调用产品->类别来获取所显示的每种产品的类别名称.
在wooCommerce文件中,我找到了content-product.php
(在templates/content-product.php中)
,我想通过添加以下操作为每种产品添加类别:
do_action( 'woocommerce_before_shop_loop_item_title' );
我知道我可以在主题的functions.php中添加一个函数,以便将一些新代码注入" woocommerce_before_shop_loop_item_title"
但是我不知道如何获取每种产品的类别.
有人可以帮忙吗?或告诉我在哪里可以找到答案?
任何帮助将不胜感激,谢谢!