1

i create category have name Brand with this code. i have total 3 categories and each have 3 subcategories.

how to output 3 category and 3 subcategories for each category?

function.php code

function insert_category() {
if(!term_exists('brand')) {
    wp_insert_term(
        'Brand',
        'category',
        array(
          'description' => 'sample category.',
          'slug'        => 'brand'
        )
    );
  }
 }
add_action( 'after_setup_theme', 'insert_category' );
2

If your question if like this:

Category A
- sub-category 1
- sub-category 2
- sub-category 3

Then you would create the following in your theme's functions.php:

//create the main category
wp_insert_term(

// the name of the category
'Category A', 

// the taxonomy, which in this case if category (don't change)
'category', 

array(

// what to use in the url for term archive
'slug' => 'category-a',  
));

Then for each sub-category:

wp_insert_term(

// the name of the sub-category
'Sub-category 1', 

// the taxonomy 'category' (don't change)
'category',

array(
// what to use in the url for term archive
'slug' => 'sub-cat-1', 

// link with main category. In the case, become a child of the "Category A" parent  
'parent'=> term_exists( 'Category A', 'category' )['term_id']

));

Hope this helps.

  • parse error, expecting at last line of subcategory, ' 'parent'=> term_exists( 'Category A', 'category' )['term_id'] ' – FRQ6692 Jul 28 '16 at 22:46
  • second code not working properly can you edit it plz.. – FRQ6692 Jul 28 '16 at 22:53
  • Make sure it's by itself, not in a function. – Gregory Schultz Jul 28 '16 at 23:51
  • @ Gregory Schultz ,, i add complete code here – FRQ6692 Jul 28 '16 at 23:59
  • Don't surround it with a function and action. – Gregory Schultz Jul 29 '16 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.