非常感兴趣
这里是自动添加条款到现有的自定义分类的代码。
假设::我假定帖子类型'book'已经注册,并且自定义分类'流派'也已经注册。
<?php
add_action('after_setup_theme', 'wpso2523951_add_taxonomy_genre');
function wpso2523951_add_taxonomy_genre(){
$custom_post_type = 'book';
$custom_taxonomy = 'genre';
$required_terms = [ 'horror'=>'Horror',
'comedy'=>'Comedy',
'fiction'=>'Fiction',
];
if(post_type_exists($custom_post_type) && taxonomy_exists($custom_taxonomy)){
register_taxonomy_for_object_type($custom_taxonomy, $custom_post_type);
foreach($required_terms as $slug=>$term_name){
wp_insert_term(
$term_name,
$custom_taxonomy,
[
'description'=> $term_name.' genre.',
'slug' => $slug,
]
);
}
}
}
?>
感谢您的回答,我尝试此代码以主题今天上午。注册post_type'book',register_taxonomy'流派'。重新激活我的主题,但没有发生,恐怖,喜剧和小说未添加到列表中 什么问题? – Yayun