Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅将脚本应用于第一级和第二级类别_Php_Wordpress_Woocommerce_Hierarchical_Taxonomy Terms - Fatal编程技术网

Php 仅将脚本应用于第一级和第二级类别

Php 仅将脚本应用于第一级和第二级类别,php,wordpress,woocommerce,hierarchical,taxonomy-terms,Php,Wordpress,Woocommerce,Hierarchical,Taxonomy Terms,我需要在不同的特定类别上添加一个php函数脚本 1个类别的示例:汽车->宝马->x1。(这只是一个示例,我有不同的类别层次结构,如下所示) 我需要将此脚本仅应用于类别“汽车”和类别“宝马”,所以第一和第二级类别只 我该怎么做?在下面,您将找到3个条件函数,允许您检查术语是否来自: 仅限一级产品类别 仅限二级产品类别 一级或二级产品类别 所有3个条件函数都使用产品类别术语Id、slug或name // Utility function: Get the WP_Term object from

我需要在不同的特定类别上添加一个php函数脚本

1个类别的示例:汽车->宝马->x1。(这只是一个示例,我有不同的类别层次结构,如下所示)

我需要将此脚本仅应用于类别“汽车”和类别“宝马”,所以第一和第二级类别只


我该怎么做?

在下面,您将找到3个条件函数,允许您检查术语是否来自:

  • 仅限一级产品类别
  • 仅限二级产品类别
  • 一级或二级产品类别
所有3个条件函数都使用产品类别术语Id、slug或name

// Utility function:  Get the WP_Term object from term ID, slug or name
function wc_category_to_wp_term( $term, $taxonomy ) {
    if( is_numeric( $term ) && term_exists( $term, $taxonomy ) ) {
        return get_term( (int) $term, $taxonomy );
    } elseif ( is_string( $term ) && term_exists( $term, $taxonomy ) ) {
        return get_term_by( 'slug', sanitize_title( $term ), $taxonomy );
    } elseif ( is_a( $term, 'WP_Term' ) && term_exists( $term->slug, $taxonomy ) ) {
        return $term;
    }
    return false;
}

// Conditional function to check if  if a product category is a top level term
function is_wc_cat_lvl_1( $category, $taxonomy = 'product_cat' ) {
    if( $term = wc_category_to_wp_term( $category, $taxonomy ) ) {
        return ( $term->parent === 0 ) ? true : false;
    }
    return false;
}

// Conditional function to check if a product category is a second level term
function is_wc_cat_lvl_2( $category, $taxonomy = 'product_cat' ) {
    if( ( $term = wc_category_to_wp_term( $category, $taxonomy ) ) && $term->parent !== 0 ) {
        $ancestors = get_ancestors( $term->term_id, $taxonomy );

        // Loop through ancestors terms to get the 1st level term
        foreach( $ancestors as $parent_term_id ){
            // Get the 1st level category
            if ( get_term($parent_term_id, $taxonomy)->parent === 0 ) {
                $first_level_id = $parent_term_id;
                break; // stop the loop
            }
        }
        return isset($first_level_id) && $first_level_id === $term->parent ? true : false;
    }
    return false;
}

// Conditional function to check if a product category is a first or second level term
function is_wc_cat_lvl_1_or_2( $category, $taxonomy = 'product_cat' ) {
    $lvl_1 = is_wc_cat_lvl_1( $category, $taxonomy = 'product_cat' );
    $lvl_2 = is_wc_cat_lvl_2( $category, $taxonomy = 'product_cat' );

    return $lvl_1 || $lvl_2 ? true : false;
}
代码进入活动子主题(或活动主题)的functions.php文件。测试和工作


用法示例-如果产品类别术语来自第一或第二产品类别级别,则显示:

$term1 = "Clothing"; // a 1st level category
$term2 = "Hoodies"; // a 2nd level category
$term3 = "Levis"; // a 3rd level category

echo  'Is "' . $term1 . '" a level 1 or 2 category: ' . ( is_wc_cat_lvl_1_or_2( $term1 ) ? 'YES' : 'NO' ) . '<br>'; 
echo  'Is "' . $term2 . '" a level 1 or 2 category: ' . ( is_wc_cat_lvl_1_or_2( $term2 ) ? 'YES' : 'NO' ) . '<br>';
echo  'Is "' . $term3 . '" a level 1 or 2 category: ' . ( is_wc_cat_lvl_1_or_2( $term3 ) ? 'YES' : 'NO' ) . '<br>'; 
$term1=“服装”//一级分类
$term2=“连帽衫”//二级分类
$term3=“李维斯”//三级分类
echo'是“.$term1.”一级或二级类别:'。(是一级还是二级?($term1)?“是”:“否”)
'; echo'是“.$term2.”“一级或二级类别:”。(是一级还是二级?($term2)?‘是’:‘否’。’
'; echo“是”.$term3.“一级或二级类别:”。(是1级还是2级?($term3)?“是”:“否”)
';
这将输出:

“服装”是一级还是二级类别:是
“连帽衫”是1级还是2级:是
“李维斯”是一级还是二级:否


欢迎来到堆栈溢出!您已经编写了哪些代码?请仔细阅读,然后在问题中加入并标记代码。干杯