我需要用一周中的天数填充自定义分类法“Days”。。我不想让客户在创建天的过程中遇到麻烦,也不想在那里删除天或拼写错误的天。根据上述建议,我想到了这个,但我想知道是否有更简洁的编码方式:
/*************************************** ...Create a Custom Taxonomy for days ******************************/
add_action( \'init\', \'build_taxonomies\', 0 );
function build_taxonomies() {
register_taxonomy(
\'days\',
\'schedule\',
array( \'hierarchical\' => true,
\'label\' => \'Days\',
\'query_var\' => true,
\'show_ui\' => false, //removes the menus from admin menu and edit panel
\'rewrite\' => true ) );
/*---------------------------------------Check to see if the days are created..if not, create them----*/
$parent_term = term_exists( \'days\', \'days\' ); // array is returned if taxonomy is given
$parent_term_id = $parent_term[\'term_id\']; // get numeric term id
wp_insert_term(//this should probably be an array, but I kept getting errors..
\'Monday\', // the term
\'days\', // the taxonomy
array(
\'slug\' => \'monday\',
\'parent\'=> $parent_term_id ));
wp_insert_term(
\'Tuesday\', // the term
\'days\', // the taxonomy
array(
\'slug\' => \'tuesday\',
\'parent\'=> $parent_term_id ));
wp_insert_term(
\'Wednesday\', // the term
\'days\', // the taxonomy
array(
\'slug\' => \'wednesday\',
\'parent\'=> $parent_term_id ));
wp_insert_term(
\'Thursday\', // the term
\'days\', // the taxonomy
array(
\'slug\' => \'thursday\',
\'parent\'=> $parent_term_id ));
wp_insert_term(
\'Friday\', // the term
\'days\', // the taxonomy
array(
\'slug\' => \'friday\',
\'parent\'=> $parent_term_id ));
wp_insert_term(
\'Saturday\', // the term
\'days\', // the taxonomy
array(
\'slug\' => \'saturday\',
\'parent\'=> $parent_term_id ));
wp_insert_term(
\'Sunday\', // the term
\'days\', // the taxonomy
array(
\'slug\' => \'sunday\',
\'parent\'=> $parent_term_id ));
}
/************ now I add my own meta box for days to get rid of extra controls *************/
add_action(\'admin_menu\', \'add_custom_categories_box\');
function add_custom_categories_box() {
add_meta_box(\'myrelateddiv\', \'Days*\', \'ilc_post_related_meta_box\', \'schedule\', \'normal\', \'low\', array( \'taxonomy\' => \'days\' ));
}
function ilc_post_related_meta_box( $post, $box ) {
$defaults = array(\'taxonomy\' => \'related\');
if ( !isset($box[\'args\']) || !is_array($box[\'args\']) )
$args = array();
else
$args = $box[\'args\'];
extract( wp_parse_args($args, $defaults), EXTR_SKIP );
$tax = get_taxonomy($taxonomy);
?>
<ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
<?php
wp_terms_checklist($post->ID, array( \'taxonomy\' => $taxonomy, \'popular_cats\' => $popular_ids, \'checked_ontop\' => FALSE ) )
?>
</ul>