正如我所评论的,如果下拉列表不存在,这将创建下拉列表,并将其保存在数据库中
remove_meta_box(\'tagsdiv-book_author\', \'book\', \'normal\');
add_meta_box(\'custom-taxonomy-author-dropdown\',\'Author\',\'author_dropdowns_box\',\'book\',\'side\',\'high\');
function author_dropdowns_box( $post ) {
wp_nonce_field(\'custom-author-dropdown\', \'author-dropdown-nonce\');
echo "Author:";
echo "<select id=\'bauthor\' name=\'bauthor[]\'>";
echo "<option value=\'0\'>None</option>";
$options = get_option(\'bauthor_dropdown\');
if ($option !== false){
echo $options;
}else{
$options = \'\';
$terms = get_terms( \'book_author\', \'hide_empty=0\');
$object_terms = wp_get_object_terms( $post->ID, \'book_author\', array(\'fields\'=>\'ids\'));
foreach ( $terms as $term ) {
if ( in_array($term->term_id, $object_terms) ) {
$parent_id = $term->term_id;
$options .= "<option value=\'".$term->term_id."\' selected=\'selected\'>".$term->name."</option>";
} else {
$options .= "<option value=\'".$term->term_id."\'>".$term->name."</option>";
}
}
update_option(\'bauthor_dropdown\',$options);
echo $options;
}
echo "</select><br />";
}
add_action(\'save_post\',\'save_my_custom_taxonomy\');
剩下的就是在添加/编辑/删除该分类法的术语时清除列表:
//clear the list when a new term is created: or edited
add_action(\'edit_book_author\',\'clear_on_add\',10,2);
add_action(\'created_book_author\',\'clear_on_add\',10,2);
function clear_on_add($term_id, $tt_id){
delete_option(\'bauthor_dropdown\');
}
//clear the list when a term is deleted:
add_action(\'delete_term\', \'clear_on_delete\',10,3);
function clear_on_delete($term, $tt_id, $taxonomy){
if ($taxonomy == "book_author"){
delete_option(\'bauthor_dropdown\');
}
}