此代码在wordpress管理区域中正确显示类别。但不显示子类别。
我需要显示3个类别和每个类别3个子类别吗?
这就是我想要的每个类别的内容:
A类
我在wordpress主题的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',
));`
然后,对于每个子类别:
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']
));
但我收到一个错误:
解析错误:解析错误,期望在第57行中出现'')''…
对应于'parent'=> term_exists( 'Category A', 'category' )['term_id']
。
我做错了什么?
问题是您需要在函数外部获取父项ID,以避免发生错误。您可以通过以下方式轻松实现:
$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
)
);
然后,您将使用其他2组子类别:
// 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'];
…以相同的方式(请注意每个子类别都具有唯一的子弹,这意味着所有9个不同的子类别弹头) …
参考:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句