[英]Wordpress getting sub categories not working
我正在尝试使用以下代码在WordPress中创建子类别:
$cid = wp_insert_term(
$term_name, // the term
'product_cat',// the taxonomy
array(
'description'=> 'asdasd',
'slug' => $term_slug,
'parent' => $parent_term_id
)
);
它在数据库中创建子类别,但是当我尝试获取所有子类别时,它显示为空数组。 这是我尝试过的:
$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);
还有:
$all_cats = get_categories($args);
请帮忙。 谢谢
试试这个,我还没有测试,但它应该工作。 您的代码无法正常工作的原因是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>';
来自食典的参考:
https://developer.wordpress.org/reference/functions/get_terms/#description
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.