J'avais besoin de remplir une "jours" de taxonomie personnalisée avec les jours de la semaine. Je ne voulais pas que le client doive jouer avec la création de jours, ou y entrer et supprimer des jours ou des jours mal orthographiés. En suivant les conseils ci-dessus, j'ai trouvé cela, mais je me demande s'il existe un moyen plus concis de le coder:
/*************************************** ...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>