当我的Wordpress主题被激活时,我在functions.php文件中创建了两个类别,如下所示:
/**
* @desc Create categories on theme activation
**/
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( 'Work' ) ) {
wp_create_category( 'Work' );
}
if ( ! get_cat_ID( 'Blog' ) ) {
wp_create_category( 'Blog' );
}
}
}
add_action ( 'after_setup_theme', 'create_my_cat' );如果工作和博客这两个类别还不存在,则创建它们,但如果它们已经存在,则什么也不做。
如果这两个新类别已经存在/创建时,我如何获取它们的ID?如果可能的话,我需要将它们存储为两个单独的变量($work和$blog),这样我就可以在同一个文件中重用它们。
发布于 2013-05-21 19:08:26
来自wordpress manual
wp_create_category( $cat_name, $parent ):$work = wp_create_category(‘工作’);$blog = wp_create_category(‘博客’);
我测试了行为,并完全按照说明书中的说明工作。
https://stackoverflow.com/questions/16667975
复制相似问题