最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Programmatically Create Category and sub Category and sub sub Category

programmeradmin4浏览0评论

I want insert category like this:

Category A

  • sub-category 1
    • sub-sub-category 2
      • sub-sub-category 3

thanks

this is my code

$term = wp_insert_term(
    $row[11], 
    'product_cat',
    [
        'description' => 'FooBar Category description', 
        'slug' => $row[11]
    ]
);
if(is_wp_error($term)) {
    $term_id = $term->error_data['term_exists'] ?? null;
} else {
    $term_id = $term['term_id'];
}
$term1 = wp_insert_term(
    $row[12], 
    'product_cat', 
    array(
        // what to use in the url for term archive
        'slug' => $row[12], 
        // link with main category. In the case, become a child of the "Category A" parent  
        'parent' => term_exists($row[11], 'product_cat')['term_id']
    )
);
$term2 = wp_insert_term($row[13], 'product_cat', 
    array(
        // what to use in the url for term archive
        'slug' => $row[13], 
        // link with main category. In the case, become a child of the "Category A" parent  
        'parent' => term_exists($row[12], 'product_cat')['term_id']
    )
);

I want insert category like this:

Category A

  • sub-category 1
    • sub-sub-category 2
      • sub-sub-category 3

thanks

this is my code

$term = wp_insert_term(
    $row[11], 
    'product_cat',
    [
        'description' => 'FooBar Category description', 
        'slug' => $row[11]
    ]
);
if(is_wp_error($term)) {
    $term_id = $term->error_data['term_exists'] ?? null;
} else {
    $term_id = $term['term_id'];
}
$term1 = wp_insert_term(
    $row[12], 
    'product_cat', 
    array(
        // what to use in the url for term archive
        'slug' => $row[12], 
        // link with main category. In the case, become a child of the "Category A" parent  
        'parent' => term_exists($row[11], 'product_cat')['term_id']
    )
);
$term2 = wp_insert_term($row[13], 'product_cat', 
    array(
        // what to use in the url for term archive
        'slug' => $row[13], 
        // link with main category. In the case, become a child of the "Category A" parent  
        'parent' => term_exists($row[12], 'product_cat')['term_id']
    )
);
Share Improve this question edited Jan 3, 2020 at 18:13 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Jan 3, 2020 at 15:55 Khairi KsouriKhairi Ksouri 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

When you trying to insert child categories you are looking for term_exists() to return an array with a key term_id.

Following the documentation for term_exists

Return Values

(mixed)

  • Returns 0 or NULL if the term does not exist.
  • Returns the term ID if no taxonomy was specified and the term exists.
  • Returns an array if the parent exists. (format: array('term_id'=>term id, 'term_taxonomy_id'=>taxonomy id))

It seems that you are missing the third argument for term_exists() and this is the ID of the previously inserted category which will be parent.

Hope that helps!

发布评论

评论列表(0)

  1. 暂无评论