如何在Woocommerce中的single-product.php上检查产品是否属于某个类别?
-
-
可能是因为您的第一个语句缺少结尾的`)`吗?应该是if(is_product_category('audio'))Might be because your first statement is missing a closing `)`? It should be `if (is_product_category('audio'))`
- 0
- 2012-12-12
- stealthyninja
-
不错,但这不是.is_product_category似乎不适用于single-product.phpGood catch, but that's not it. is_product_category doesn't seem to work on single-product.php
- 0
- 2012-12-12
- Alex
-
5 个回答
- 投票数
-
- 2012-12-18
在这种情况下,我认为
get_categories()
并不是您的最佳选择,因为它返回一个字符串,其中所有类别均列为锚标记,适合显示,但不适用于计算在代码中列出了类别.好的,所以您需要做的第一件事就是获取当前页面的产品/帖子对象(如果您还没有的话):global $post;
然后,您可以获取产品的产品类别术语对象(类别).在这里,我将类别术语对象转换为名为
$categories
的简单数组,这样更容易查看分配了哪些块.请注意,这将返回分配给产品的所有类别,而不仅仅是当前页面之一,即,如果我们位于/shop/audio/funzo/
:$terms = wp_get_post_terms( $post->ID, 'product_cat' ); foreach ( $terms as $term ) $categories[] = $term->slug;
然后,我们只需要检查列表中是否包含类别:
if ( in_array( 'audio', $categories ) ) { // do something
将它们放在一起:
<?php global $post; $terms = wp_get_post_terms( $post->ID, 'product_cat' ); foreach ( $terms as $term ) $categories[] = $term->slug; if ( in_array( 'audio', $categories ) ) { echo 'In audio'; woocommerce_get_template_part( 'content', 'single-product' ); } elseif ( in_array( 'elektro', $categories ) ) { echo 'In elektro'; woocommerce_get_template_part( 'content', 'single-product' ); } else { echo 'some blabla'; }
希望这是您要寻找的并回答您的问题.
I don't think
get_categories()
is the best option for you in this case because it returns a string with all the categories listed as anchor tags, fine for displaying, but not great for figuring out in code what the categories are. Ok, so the first thing you need to do is grab the product/post object for the current page if you don't already have it:global $post;
Then you can get the product category term objects (the categories) for the product. Here I'm turning the category term objects into a simple array named
$categories
so it's easier to see what slugs are assigned. Note that this will return all categories assigned to the product, not just the one of the current page, ie if we're on/shop/audio/funzo/
:$terms = wp_get_post_terms( $post->ID, 'product_cat' ); foreach ( $terms as $term ) $categories[] = $term->slug;
Then we just have to check whether a category is in the list:
if ( in_array( 'audio', $categories ) ) { // do something
Putting it all together:
<?php global $post; $terms = wp_get_post_terms( $post->ID, 'product_cat' ); foreach ( $terms as $term ) $categories[] = $term->slug; if ( in_array( 'audio', $categories ) ) { echo 'In audio'; woocommerce_get_template_part( 'content', 'single-product' ); } elseif ( in_array( 'elektro', $categories ) ) { echo 'In elektro'; woocommerce_get_template_part( 'content', 'single-product' ); } else { echo 'some blabla'; }
Hopefully this is what you were looking for and answers your question.
-
- 2012-12-18
在这种情况下,has_term
应该可以工作:if ( has_term( 'audio', 'product_cat' ) ) { echo 'In audio'; woocommerce_get_template_part( 'content', 'single-product' ); } elseif ( has_term( 'elektro', 'product_cat' ) ) { echo 'In elektro'; woocommerce_get_template_part( 'content', 'single-product' ); } else { echo 'some blabla'; }
has_term
should work in this case:if ( has_term( 'audio', 'product_cat' ) ) { echo 'In audio'; woocommerce_get_template_part( 'content', 'single-product' ); } elseif ( has_term( 'elektro', 'product_cat' ) ) { echo 'In elektro'; woocommerce_get_template_part( 'content', 'single-product' ); } else { echo 'some blabla'; }
-
- 2015-02-18
值得注意的是,您可以通过调用数组来遍历选项列表,而不必假设很多情况下都需要通过其他elseif检查来弄乱代码.
>if( has_term( array( 'laptop', 'fridge', 'hats', 'magic wand' ), 'product_cat' ) ) : // Do stuff here else : // Do some other stuff endif;
It's worth noting that you can go through a list of options by calling an array rather than having to clutter up your code with lots of elseif checks, assuming you want to do the same thing with each category that is.
if( has_term( array( 'laptop', 'fridge', 'hats', 'magic wand' ), 'product_cat' ) ) : // Do stuff here else : // Do some other stuff endif;
-
我认为应该将此答案作为编辑添加到Milo的答案中.I think this answer should be added, as edittion, to Milo's answer.
- 0
- 2015-02-18
- cybmeta
-
- 2016-06-09
这很古老,但以防万一人们仍然将WooThemes作为简单的解决方案:
if ( is_product() && has_term( 'your_category', 'product_cat' ) ) { //do code }
*将"您的类别"更改为您正在使用的任何内容.
以下是文档的链接: https://docs.woothemes.com/document/remov-product-content-based-on-category/
This is old but just in case people are still looking WooThemes as a simple solution:
if ( is_product() && has_term( 'your_category', 'product_cat' ) ) { //do code }
*Change 'your_category' to whatever you are using.
Here is the link to the documentation: https://docs.woothemes.com/document/remov-product-content-based-on-category/
-
- 2012-12-12
I'd look at using the
get_categories()
function of the WC_Product class.You can find the link to the documentation here.
Basically within the loop of the page call the function to return the categories associated with the product.
-
我无法对此进行编码.我不知道如何使它工作.有人请说明一下.我尽力了.我应该用get_categories()代替它吗?I am not able to code this. I don't have a clue how to get this to work. Somebody please illustrate this. I tried my best up there. Should I replace this with get_categories()?
- 0
- 2012-12-13
- Alex
-
如果您位于产品类别页面上,则@Alexis_product_category()函数将返回TRUE.不是产品类别.我现在正在处理一个项目,但是稍后我将尝试为您提供一个代码片段.@Alex the is_product_category() function returns TRUE if you're on the product category page. Not the category of the product. I'm heads down on a project right now, but I'll try to get you a code snippet later.
- 0
- 2012-12-13
- Steve
-
谢谢,史蒂文(Steven)花时间编码这个小片段.非常感谢.Thanks, Steven for taking time to code this little snippet. Appreciate it very much.
- 0
- 2012-12-13
- Alex
我如何才能在 single-product.php 上检查产品是否属于某个产品类别?
is_product_category('slug')对 single-product.php 没有影响.我想有较高的条件.在单个产品页面上对此有任何解决方案吗?