Custom taxonomy and children taxonomies
-
I’m trying to add some custom taxonomies and child taxonomies to a CPT plugin that I recently created. I can get the parent taxonomies to import from my plugin but not the child taxonomies with descriptions. Here’s the code I’ve got so far:
$categories = array( 'Category 01'=>'<a href="https://category01.com" target="_blank">Category 01</a>', ); foreach($categories as $category => $cat_desc){ wp_insert_term( $category, // the term 'style', // the taxonomy array( 'description'=> $cat_desc, 'slug' => $category , // 'parent'=> $parent_term_id ) ); }; // List out all Categories with subcategories $styles = array( 'Category 01' => array( 'Subcategory 1A' => 'Subcategory 1A Description<br><a href="https://www.mysite.com/style01.php#1a" target="_blank">Subcategory 1A Description</a>', 'Subcategory 1B' => 'Subcategory 1B Description<br><a href="https://www.mysite.com/style01.php#1a" target="_blank">Subcategory 1B Description</a>', 'Subcategory 1C' => 'Subcategory 1C Description<br><a href="https://www.mysite.com/style01.php#1a" target="_blank">Subcategory 1C Description</a>', 'Subcategory 1D' => 'Subcategory 1D Description<br><a href="https://www.mysite.com/style01.php#1a" target="_blank">Subcategory 1D Description</a>', 'Subcategory 1E' => 'Subcategory 1E Description<br><a href="https://www.mysite.com/style01.php#1a" target="_blank">Subcategory 1E Description</a>', ) ); // Import each array as taxonomy with parent foreach($styles as $parent_term => $styles ){ $parent_term = term_exists( $parent_term , 'styles' ); // array is returned if taxonomy is given $parent_term_id = $parent_term['term_id']; // get numeric term id foreach($styles as $style => $style_desc){ wp_insert_term( $style, // the term works 'style', // the taxonomy works array( 'description'=> $style_desc, // does not work 'slug' => $style , //works 'parent'=> $parent_term_id // does not work ) ); } };
How do I run the second script to attach the child taxonomy to the parent? Would it be better to have nested arrays for the taxonomy and child taxonomy instead of running two wp_instert_term loops?
- The topic ‘Custom taxonomy and children taxonomies’ is closed to new replies.