【问题标题】:Create Taxonomy To Custom Post Type After Theme Activation主题激活后为自定义帖子类型创建分类
【发布时间】:2017-06-19 00:16:10
【问题描述】:

我需要帮助 如何在主题激活后为自定义帖子类型创建分类? 示例案例: 我的自定义帖子类型有自定义帖子类型 'book' 和自定义分类法 'genre'。这是向 functions.php 添加代码的简单方法,但我需要在主题激活时自动创建 horor、comedy、fiction 到“流派”(after_theme_setup)

对这个话题很感兴趣Creating Wordpress Category at the time of Theme Activation

【问题讨论】:

    标签: wordpress custom-taxonomy


    【解决方案1】:

    这是自动将术语添加到现有自定义分类的代码。

    假设:: 我假设帖子类型“书”已经注册,并且自定义分类法“流派”也已经注册。

    <?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 '书', register_taxonomy '流派'。重新激活我的主题,但没有任何反应,恐怖、喜剧和小说未添加到列表中怎么了?
    猜你喜欢
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 1970-01-01
    • 1970-01-01
    • 2020-02-28
    • 2018-11-21
    • 2020-01-19
    相关资源
    最近更新 更多