ich erstelle eine Kategorie mit dem Namen Brand mit diesem Code. Ich habe insgesamt 3 Kategorien und jeweils 3 Unterkategorien.
Wie werden 3 Kategorien und 3 Unterkategorien für jede Kategorie ausgegeben?
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' );
Wenn deine Frage so ist:
Kategorie A
- Unterkategorie 1
- Unterkategorie 2
- Unterkategorie 3
Dann würden Sie im functions.php
Ihres Themas Folgendes erstellen:
//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',
));
Dann für jede Unterkategorie:
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']
));
Hoffe das hilft.