我目前在一个网站上工作,该网站有一个自定义的帖子类型(“时间表”),需要将自定义的层次分类法(在本例中称为“位置”和“服务类型”)显示为单选按钮,而不是清单,如中所示this tutorial.
在这里,我必须修改代码,以防止显示仅由其他自定义帖子类型使用的分类法的某些术语,而不是此类型。这就是我不想使用插件的原因。
然而,每当我尝试使用基于单选按钮的分类系统更新或保存此帖子类型中的帖子时,所选的分类术语都不会保存到帖子中。
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
}