简体   繁体   English

从 WooCommerce 中的当前产品类别中获取兄弟术语 ID 列表

[英]Get a list of siblings term ids from current product category in WooCommerce

I want to retrieve a list of term Ids based on the current category ID.我想根据当前类别 ID 检索术语 ID 列表。

At the moment I'm using the following code:目前我正在使用以下代码:

$product_cat_items  = get_queried_object();
$product_cat_id     = $product_cat_items->term_id;
$product_cat_child  = get_term($product_cat_id, 'product_cat');
$product_cat_parent = $product_cat_child->parent;

$product_cat_related= get_terms('product_cat', array( 'parent' => $product_cat_parent, 'exclude' => $product_cat_id ));

It's working and I get an array of the terms.它正在工作,我得到了一系列条款。 But the probem is, that I only need the IDs from the term object to get a list like this:但问题是,我只需要术语对象中的 ID 即可获得如下列表:

123,345,678 123,345,678

Is there any way to extract such a list from the $product_cat_related array?有没有办法从$product_cat_related数组中提取这样的列表?

This is the current output:这是当前的输出:

array(2) {
  [0]=>
  object(WP_Term)#26238 (10) {
    ["term_id"]=>
    int(177)
    ["name"]=>
    string(27) "Name"
    ["slug"]=>
    string(21) "name"
    ["term_group"]=>
    int(0)
    ["term_taxonomy_id"]=>
    int(177)
    ["taxonomy"]=>
    string(11) "product_cat"
    ["description"]=>
    string(0) ""
    ["parent"]=>
    int(140)
    ["count"]=>
    int(8)
    ["filter"]=>
    string(3) "raw"
  }
  [1]=> ....
}

I found a solution here: https://stackoverflow.com/a/25286095/1788961我在这里找到了一个解决方案: https : //stackoverflow.com/a/25286095/1788961

For me this code works:对我来说,这段代码有效:

$idCats = array_column($product_cat_related, 'term_id');

Since WordPress version 4.5.0, taxonomies should be passed via the "taxonomy" argument in the $args array ( see get_terms() documentation ) .自 WordPress 4.5.0 版起,分类法应通过$args数组中的“分类法”参数传递请参阅get_terms()文档
Also get_queried_object() already gives a WP_Term Object when the queried Object is a taxonomy term.当查询的对象是分类术语时, get_queried_object()已经给出了一个WP_Term对象。
Also you can use 'fields' => 'ids' as an argument in get_terms() , to get only an array of term Ids instead of an array of WP_term Objects (see WP_Term_Query available arguments ) .您也可以使用'fields' => 'ids'作为get_terms()的参数,以仅获取术语 Id数组而不是WP_term对象数组(请参阅WP_Term_Query可用参数
To finish, you will use PHP implode() to get a string of coma separated terms Ids.最后,您将使用 PHP implode()来获取一串由昏迷分隔的术语 Id。

So your code will be instead:所以你的代码将改为:

$current_term = get_queried_object(); // Already a WP_Term Object

if ( $current_term->parent > 0 ) {
    $siblings_ids = get_terms( array(
        'taxonomy'  => 'product_cat',
        'parent'    => $current_term->parent,
        'exclude'   => $current_term->term_id,
        'fields'    => 'ids',
    ) );

    // Get a string of coma separated terms Ids
    $siblings_list_ids = implode(',', $siblings_ids);

    // Testing output
    echo $siblings_list_ids;
}

Tested and works.测试和工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM