问题描述
我将创建一个函数,该函数将检查名为感言的类别是否已经可用。如果可用,请注意,如果不可用,请创建一个名为推荐书的新类别。我正在使用以下代码,但是在激活主题时没有任何反应。
I am going to create a function that will check whether if a Category named Testimonials is already available or not. If it is available do noting, whereas if it is not there, then create a new Category named Testimonials. I am using following code but nothing happened at the time of theme activation. What is missing?
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');
推荐答案
操作 create_category
在创建新类别时运行。
The action create_category
runs when a new category is created.
您希望类别创建功能在激活主题时运行。相关操作是。
You want your category creation function to run when the theme is activated. The relevant action is after_setup_theme
.
将其放在主题的 functions.php 中,您应该会很好:
Drop this in your theme's functions.php and you should be good to go:
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' );
这篇关于主题激活时创建Wordpress类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!