簡體   English   中英

WordPress獲取子類別不起作用

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM