2017-06-19 39 views
1

我需要帮助 如何在主题激活后创建自定义发布类型的分类标准? 示例案例: 我有自定义帖子类型'书籍'和自定义分类标准'流派'我的自定义帖子类型。这是很容易的方式,当代码添加到的functions.php,但我需要自动创建horor,喜剧,科幻为“流派”的主题时激活(after_theme_setup)在主题激活后创建分类标准到自定义发布类型

这一主题Creating Wordpress Category at the time of Theme Activation

回答

0
非常感兴趣

这里是自动添加条款到现有的自定义分类的代码。

假设::我假定帖子类型'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, 
           ] 
          ); 
        } 

      } 
     } 
?> 
+0

感谢您的回答,我尝试此代码以主题今天上午。注册post_type'book',register_taxonomy'流派'。重新激活我的主题,但没有发生,恐怖,喜剧和小说未添加到列表中 什么问题? – Yayun

相关问题