微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – woocommerce类别深度作为if语句的条件

我不确定这里继续前进的最佳方式.

我有这样的类别(简化版):

-fruit
--apple
---large
---small

--banana
---var1
----large
----small
---var2
----small
----large

我想使用类别的深度作为if语句中的条件来实现这样的事情:

如果类别是深度2(苹果>大),请执行此操作

否则,如果类别是深度3(香蕉> var1>小),则执行其他操作.

香港专业教育学院尝试过使用这里的功能,但除了一个空数组之外,我一直无法获得任何回报! http://www.devdevote.com/cms/wordpress-hacks/get_depth.html

解决方法:

您可以在返回的项目上使用get_ancestors()然后使用count().从那里将是简单的逻辑.

作为一个功能,这将看起来像:

function so50409656_count_ancestors( $object_id, $object_type ='', $resource_type='' ) {

    $terms = get_ancestors( $object_id, $object_type, $resource_type );

    return count($terms) + 1;

}

然后像这样调用

$term_depth = so50409656_count_ancestors( $term->term_id, 'your_term_type', 'taxonomy' );

if ( $term_depth === 1 ) :
    // ..your top level ancestor action
elseif ( $term_depth === 2 ) :
    // ..your direct child choice of actions here
elseif ( $term_depth === 3 ) :
    // ..your second level child action here
endif;

编辑:

澄清1的结果将是顶级祖先,根据OP请求,如大(在apple> large)等于2

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐