Friday, June 7, 2013

How to load several hierarchical categories from functions.php

How to load several hierarchical categories from functions.php

I need to insert several categories (about 400) into my site. The categories are hierarchical.
I would like to load them from functions.php but now I can add one category at time, like this:
add_action('admin_init', 'load_categories');
function load_categories(){
$cat = array(
    'cat_ID' => 0,
    'cat_name' => 'Personale',
    'category_description' => 'La mia categoria',
    'category_nicename' => 'personale',
    'category_parent' => 0,
    'taxonomy' => 'category'
);
wp_insert_category($cat);
}
I would check if category exist and, if not, add it.
My hierarchical structure is like this:
category

sub-parent-category


sub-sub-parent-category
Is this a best practise to load category by functions.php with admin_init action?
I'm sorry but I'm in developer phase at the time and often I clean the database due to tests

No comments:

Post a Comment