在主题激活时创建Wordpress类别

Kas*_*que 1 wordpress categories

我将创建一个函数来检查名为Testimonials的类别是否已经可用.如果可用,请注意,如果不存在,则创建一个名为Testimonials的新类别.我正在使用以下代码,但在主题激活时没有任何反应.缺什么?

function create_my_cat () {
    if (file_exists (ABSPATH.'/wp-admin/includes/taxonomy.php')) {
        require_once (ABSPATH.'/wp-admin/includes/taxonomy.php');    
        if (!get_cat_ID('testimonials')) {
            wp_create_category('testimonials');
        }
    }
}
add_action ('create_category', 'create_my_cat');
Run Code Online (Sandbox Code Playgroud)

Joh*_*lle 7

create_category创建新类别时将运行该操作.

您希望在激活主题时运行类别创建功能.相关行动是after_setup_theme.

把它放在主题的functions.php中,你应该好好去:

function create_my_cat () {
    if (file_exists (ABSPATH.'/wp-admin/includes/taxonomy.php')) {
        require_once (ABSPATH.'/wp-admin/includes/taxonomy.php'); 
        if ( ! get_cat_ID( 'Testimonials' ) ) {
            wp_create_category( 'Testimonials' );
        }
    }
}
add_action ( 'after_setup_theme', 'create_my_cat' );
Run Code Online (Sandbox Code Playgroud)