简体   繁体   English

WordPress获取子类别不起作用

[英]Wordpress getting sub categories not working

I am trying to create subcategories in WordPress using this code: 我正在尝试使用以下代码在WordPress中创建子类别:

 $cid = wp_insert_term(
     $term_name, // the term 
    'product_cat',// the taxonomy
    array(
        'description'=> 'asdasd',
        'slug' => $term_slug,
        'parent' => $parent_term_id
    )
);

it creates subcategory in database but when I tried to get all subcategories it shows empty array. 它在数据库中创建子类别,但是当我尝试获取所有子类别时,它显示为空数组。 This is what I have tried yet: 这是我尝试过的:

$args = array(
 'orderby' => 'name',
 'order' => 'ASC',
 'hide_empty' => 0,
 'name' => '',
 'parent' => $parent_term_id
);
$terms_sub = get_terms('product_cat', $args);
print_r($terms_sub);

And this as well: 还有:

$all_cats = get_categories($args);

Please help. 请帮忙。 Thanks 谢谢

Try this i have not tested it, but it should work. 试试这个,我还没有测试,但它应该工作。 The reason your code is not working is that wordpress has changed the way it works from version 4.5.0. 您的代码无法正常工作的原因是wordpress从4.5.0版本开始更改了其工作方式。

$terms_sub = get_terms( 
    array(
        'taxonomy'   => 'product_cat',
        'orderby'    => 'name',
        'order'      => 'ASC',
        'hide_empty' => false,
        'parent'     => $parent_term_id
    )
);
echo '<pre>';
print_r($terms_sub);
echo '</pre>';

Reference from Codex: 来自食典的参考:

https://developer.wordpress.org/reference/functions/get_terms/#description https://developer.wordpress.org/reference/functions/get_terms/#description

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

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