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

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...