简体   繁体   中英

Wordpress getting sub categories not working

I am trying to create subcategories in WordPress using this code:

 $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.

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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