В настоящее время я работаю над веб-сайтом с пользовательским типом публикации («расписания»), для которого требуется, чтобы пользовательские иерархические таксономии (в данном случае называемые «расположение» и «тип обслуживания») отображались в виде переключателей вместо контрольных списков, как показано в этом руководстве .
Здесь я должен был изменить код, чтобы не показывать определенные термины таксономии, которые используются только другими пользовательскими типами постов, а не этот, чтобы показывать это. , Вот почему я не хотел использовать плагин для этого.
Однако происходит то, что всякий раз, когда я пытаюсь обновить или сохранить сообщение из этого типа, используя систему таксономии, основанную на переключателях, выбранные условия таксономии не сохраняются в сообщении.
PHP:
add_action( 'admin_menu', 'braintrain_remove_metaboxes');
function braintrain_remove_metaboxes(){
remove_meta_box('locationdiv','schedules', 'normal');
remove_meta_box('servicetypediv', 'schedules', 'normal');
}
add_action( 'add_meta_boxes', 'braintrain_add_metaboxes');
function braintrain_add_metaboxes() {
add_meta_box( 'location_selector', 'Locations','braintrain_locations_metabox', 'schedules' ,'side','core');
add_meta_box( 'servicetype_selector', 'Services', 'braintrain_servicetype_metabox', 'schedules' ,'side','core');
}
function braintrain_locations_metabox( $post ) {
$taxonomy = 'location';
$tax = get_taxonomy($taxonomy);
$terms = get_terms($taxonomy,array('hide_empty' => 0));
$name = 'tax_input[' . $taxonomy . ']';
$popular = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
$postterms = get_the_terms( $post->ID,$taxonomy );
$current = ($postterms ? array_pop($postterms) : false);
$current = ($current ? $current->term_id : 0);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv" >
<div id="<?php echo $taxonomy; ?>-all" class="tabs-panel" style="border:0; overflow:visible; background-color: transparent; max-height: 100%; padding: 0; margin:0;">
<ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy; ?> categorychecklist form-no-clear" style="margin:0;">
<?php foreach($terms as $term) { $termChildren = get_term_children($term->term_id, $taxonomy);
$id = $taxonomy.'-'.$term->term_id;
if ( count($termChildren) === 0 ) {
echo "<li id='$id'><label class='selectit'>";
echo "<input type='radio' id='in-$id' name='{$name}'".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />";
echo "</label></li>";
}
} ?>
</ul>
</div>
</div>
<?php
}
function braintrain_servicetype_metabox( $post ) {
$taxonomy = 'servicetype';
$tax = get_taxonomy($taxonomy);
$terms = get_terms($taxonomy,array('hide_empty' => 0, 'parent' => 0));
$name = 'tax_input[' . $taxonomy . ']';
$popular = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
$postterms = get_the_terms( $post->ID,$taxonomy );
$current = ($postterms ? array_pop($postterms) : false);
$current = ($current ? $current->term_id : 0);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv">
<div id="<?php echo $taxonomy; ?>-all" class="tabs-panel" style="border:0; overflow:visible; background-color: transparent; max-height: 100%; padding: 0; margin:0;">
<ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy; ?> categorychecklist form-no-clear" style="margin:0;">
<?php foreach($terms as $term){
$id = $taxonomy.'-'.$term->term_id; $termChildren = get_term_children($term->term_id, $taxonomy);
if ( count($termChildren) >= 0 ) {
echo "<li id='$id'><label class='selectit'>";
echo "<input type='radio' id='in-$id' name='{$name}'".checked($current,$term->term_id,false)."value='$term->term_id' />$term->name<br />";
echo "</label></li>";
}
}?>
</ul>
</div>
</div>
<?php
}