我正在尝试在分类法中创建一个新类别。php调用\'USSERS\'. 我在网上跟踪了几个例子,直到这一部分。
function insert_term( $name,$tax,$parent=\'\' ,$slug=\'\') {
$term_id = term_exists( $name, $tax);
if ( !$term_id)
$term_id = wp_insert_term( $name, $tax, array(\'parent\'=>$parent,\'slug\'=>$ludg) );
return $term_id;
}
insert_term(\'USSERS\',\'category\');
完成上述代码后,我去查看wordpress仪表板,看看我是否确实创建了类别,但它没有显示出来。因此,现在我感到困惑和迷茫,因为我不知道如何继续上面的代码并将其显示在仪表板上。
FUNCTIONS.PHP
<?php
if ( function_exists(\'register_sidebar\') )
register_sidebar();
add_filter( \'auto_update_plugin\', \'__return_false\' );
add_filter( \'auto_update_theme\', \'__return_false\' );
?>
function yourtheme_init() {
insert_term(\'USSERS\',\'category\');
}
add_action(\'init\', \'yourtheme_init\');
function insert_term( $name,$tax,$parent=\'\' ,$slug=\'\') {
$term_id = term_exists( $name, $tax);
if ( !$term_id) {
$term_id = wp_insert_term( $name, $tax, array(\'parent\'=>$parent,\'slug\'=>$slug) );
}
return $term_id;
}
最合适的回答,由SO网友:Alexander Holsgrove 整理而成
您在哪里调用此代码?您需要将其添加到一个init挂钩中。
如果我将其添加到插件或主题初始化中,那么它对我来说添加得很好$slug
Mike指出的拼写错误已更正。
echo \'<p>Debug: Loaded functions.php</p>\';
function yourtheme_init() {
echo \'<p>Debug: called yourtheme_init</p>\';
insert_term(\'USSERS\',\'category\');
}
add_action(\'init\', \'yourtheme_init\');
function insert_term($name,$tax,$parent=\'\', $slug=\'\') {
echo \'<p>Debug: called insert_term</p>\';
$term_id = term_exists($name, $tax);
if (!$term_id) {
echo \'<p>Debug: adding new term</p>\';
$term_id = wp_insert_term( $name, $tax, array(\'parent\'=>$parent,\'slug\'=>$slug) );
echo \'<p>Debug: new term added with ID \' . $term_id . \'</p>\';
} else {
echo \'<p>Debug: term already exists with ID \' . $term_id . \'</p>\';
}
die(\'<p>Debug: end</p>\');
return $term_id;
}