【问题标题】:Get the parent product categories ids from a sub product category ID in Woocommerce从 Woocommerce 中的子产品类别 ID 获取父产品类别 ID
【发布时间】:2018-08-31 09:56:21
【问题描述】:
For example : 
- A = 21
  - B = 22
     - C = 23

如何使用 23 个子 ID 获得 21 和 22 个 ID?

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy taxonomy-terms


    【解决方案1】:

    更新(2020 年)

    要从产品类别术语 ID 中获取父术语 ID,请尝试以下操作(代码已注释)

    // Get the parent term slugs list
    $parent_terms_list = get_term_parents_list( 23, 'product_cat', array('format' => 'slug', 'separator' => ',', 'link' => false, 'inclusive' => false) );
    
    $parent_terms_ids = []; // Initialising variable
    
    // Loop through parent terms slugs array to convert them in term IDs array
    foreach( explode(',', $parent_terms_list) as $term_slug ){
        if( ! empty($term_slug) ){
            // Get the term ID from the term slug and add it to the array of parent terms Ids
            $parent_terms_ids[] = get_term_by( 'slug', $term_slug, 'product_cat' )->term_id;
        }
    }
    
    // Test output of the raw array (just for testing)
    print_r($parent_terms_ids);
    

    经过测试并且有效。


    加法:

    您可以更好地使用 Wordpress get_ancestors() 专用功能,例如 this recent answer threadthose other related answers

    在这种情况下,代码将是:

    // Get the parent term ids array
    $parent_terms_ids = $parent_ids = get_ancestors( $child_id, 'product_cat' , 'taxonomy');  
    
    // Test output of the raw array (just for testing)
    print_r($parent_terms_ids);
    

    相关胎面:

    相关文档化的 Wordpress 函数:

    【讨论】:

    • 哇,谢谢,工作正常 :) 我还研究了波纹管函数,它工作正常 :) function parentIDs($sub_category_id) { static $parent_ids = []; if ( $sub_category_id != 0 ) { $category_parent = get_term( $sub_category_id, 'product_cat' ); $parent_ids[] = $category_parent->term_id; parentIDs($category_parent->parent); } 返回 $parent_ids; } $sub_category_id = 23; $parent_ids_array = parentIDs($sub_category_id);回声“
      ”; print_r($parent_ids_array);回声“
      ”;
    【解决方案2】:
    function parentIDs($sub_category_id)
    {
        static $parent_ids = [];        
        if ( $sub_category_id != 0 ) {
            $category_parent = get_term( $sub_category_id, 'product_cat' );             
            $parent_ids[] = $category_parent->term_id;
            parentIDs($category_parent->parent);
        } 
        return $parent_ids;
    }
    $sub_category_id = 23;
    $parent_ids_array = parentIDs($sub_category_id);
    echo "<pre>";
    print_r($parent_ids_array);
    echo "</pre>";
    

    【讨论】:

      【解决方案3】:

      获取父母 ID 的最快方法是使用 Wordpress 给出并记录的内置函数。见get_ancestors

      如果您检查了get_term_parents_list,您将看到它使用get_ancestors,请参阅此链接 https://core.trac.wordpress.org/browser/tags/5.4/src/wp-includes/category-template.php#L1362

      所以简短的答案就在代码下方。

      $parent_ids = get_ancestors( $child_id, 'product_cat' , 'taxonomy');  
      

      【讨论】:

        猜你喜欢
        • 2016-01-09
        • 2015-05-27
        • 2016-03-20
        • 1970-01-01
        • 1970-01-01
        • 2017-04-01
        • 1970-01-01
        • 2014-06-27
        • 1970-01-01
        相关资源
        最近更新 更多