我有以下代码来添加一个带有新分类法的下拉框,当前创建新帖子时的默认分类法是None. 例如,有没有一种方法可以定义默认值News.
function create_theme_taxonomy() {
if (!taxonomy_exists(\'type\')) {
register_taxonomy( \'type\', \'post\', array( \'hierarchical\' => false, \'label\' => __(\'Article Type\'), \'query_var\' => \'type\', \'rewrite\' => true ));
}
}
function add_type_box() {
add_meta_box(\'type_box_ID\', __(\'Article Type\'), \'article_type\', \'post\', \'side\', \'core\');
remove_meta_box(\'tagsdiv-type\',\'post\',\'core\');
}
function add_type_menus() {
if ( ! is_admin() )
return;
add_action(\'admin_menu\', \'add_type_box\');
}
add_type_menus();
// This function gets called in edit-form-advanced.php
function article_type($post) {
echo \'<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="\' .
wp_create_nonce( \'taxonomy_type\' ) . \'" />\';
// Get all type taxonomy terms
$types = get_terms(\'type\', \'hide_empty=0\');
?>
<select name=\'post_type\' id=\'post_type\'>
<!-- Display types as options -->
<?php
$names = wp_get_object_terms($post->ID, \'type\');
?>
<option class=\'type-option\' value=\'\'
<?php if (!count($names)) echo "selected";?>>None</option>
<?php
foreach ($types as $type) {
if (!is_wp_error($names) && !empty($names) && !strcmp($type->slug, $names[0]->slug))
echo "<option class=\'type-option\' value=\'" . $type->slug . "\' selected>" . $type->name . "</option>\\n";
else
echo "<option class=\'type-option\' value=\'" . $type->slug . "\'>" . $type->name . "</option>\\n";
}
?>
</select>
<?php
}
最合适的回答,由SO网友:Vinod Dalvi 整理而成
这是以下新更新的article\\u type函数,可使其正常工作:
我已经从“无”选项中删除了“选定属性”,并将“选定属性”添加到具有新闻段塞的术语中。
// This function gets called in edit-form-advanced.php
function article_type($post) {
echo \'<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="\' .
wp_create_nonce( \'taxonomy_type\' ) . \'" />\';
// Get all type taxonomy terms
$types = get_terms(\'type\', \'hide_empty=0\');
?>
<select name=\'post_type\' id=\'post_type\'>
<!-- Display types as options -->
<?php
$names = wp_get_object_terms($post->ID, \'type\');
?>
<option class=\'type-option\' value=\'\'>None</option>
<?php
foreach ($types as $type) {
if (!is_wp_error($names) && !empty($names) && !strcmp($type->slug, $names[0]->slug))
echo "<option class=\'type-option\' value=\'" . $type->slug . "\' selected>" . $type->name . "</option>\\n";
else if(!strcmp($type->slug, \'news\'))
echo "<option class=\'type-option\' value=\'" . $type->slug . "\' selected>" . $type->name . "</option>\\n";
else
echo "<option class=\'type-option\' value=\'" . $type->slug . "\'>" . $type->name . "</option>\\n";
}
?>
</select>
<?php
}