2016-07-29 16 views
3

Dieser Code zeigt ordnungsgemäß Kategorie in Wordpress Admin-Bereich. aber nicht Unterkategorie.Erstellen Sie Kategorien und mehrere Unterkategorien programmgesteuert

Ich muss 3 Kategorien und 3 Unterkategorien für jede Kategorie anzeigen?

Das ist, was ich für jede Kategorie haben mag:

Kategorie A

  • Unterkategorie 1
  • Unterkategorie 2
  • Unterkategorie 3

Ich habe den folgenden Code in wordpress theme's functions.php Datei hinzufügen:

//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 gilt 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'] 

)); 

Aber ich erhalte eine Fehlermeldung:

Parse error: parse error, expecting `')'' in line 57 …

zu 'parent'=> term_exists('Category A', 'category')['term_id'] entspricht.

Was mache ich falsch?

+0

Parse-Fehler in der Subkategorie Funktion erwarten. – FRQ6692

Antwort

4

Das Problem ist, dass Sie die übergeordnete ID außerhalb der Funktion erhalten müssen, um den Fehler zu vermeiden. Sie können es leicht auf diese Weise tun:

// For subcategory group of Category B 
$parent_term_b = term_exists('Category B', 'category'); 
$parent_term_b_id = $parent_term_b['term_id']; 

// For subcategory group of Category C 
$parent_term_c = term_exists('Category C', 'category'); 
$parent_term_c_id = $parent_term_c['term_id']; 

... In gleicher Weise (die Pflege eine einzigartige Slug zu haben:

$parent_term_a = term_exists('Category A', 'category'); // array is returned if taxonomy is given 
$parent_term_a_id = $parent_term_a['term_id']; // get numeric term id 

// First subcategory 
wp_insert_term(
    'Sub-category 1', // the term 
    'category', // the taxonomy 
    array(
     // 'description'=> 'Some description.', 
     'slug' => 'sub-cat-1a', 
     'parent'=> $parent_term_a_id 
    ) 
); 

// Second subcategory 
wp_insert_term(
    'Sub-category 2', // the term 
    'category', // the taxonomy 
    array(
     // 'description'=> 'Some description.', 
     'slug' => 'sub-cat-2a', 
     'parent'=> $parent_term_a_id 
    ) 
); 

// Third subcategory 
wp_insert_term(
    'Sub-category 3', // the term 
    'category', // the taxonomy 
    array(
     // 'description'=> 'Some description.', 
     'slug' => 'sub-cat-3a', 
     'parent'=> $parent_term_a_id 
    ) 
); 

dann für andere 2 Gruppen der Unterkategorien Sie verwenden für jede Unterkategorie, bedeuten, dass bei allen 9 verschiedenen Unter Butzen) ...

Referenz:

+0

wow das ist perfekt funktionieren ,, – FRQ6692

+0

müssen addiere 3 Unterkategorien für Kategorie A .. in anderen Wort 3 Unterkategorie für 1 – FRQ6692

1

Es sieht aus wie Sie das erste Zitat aus dem Namen der übergeordneten Kategorie sind vermisst und für den Parse-Fehler erklären könnten, was sein soll:

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

Edited Kommentar:

$parent = term_exists('Category A', 'category'); 
$termId = $parent['term_id']; 

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'=> $termId 

)); 
+0

Ich bearbeite meine Frage .. ich vermisse das, während ich schreibe. – FRQ6692

+0

Fehler in dieser Zeile '' übergeordnete '=> term_exists (' Kategorie A ',' Kategorie ') [' term_id ']' aber ich kann es nicht lösen .. – FRQ6692

+0

Können Sie den tatsächlich angezeigten Fehler posten? – phpchap

Verwandte Themen