admin管理员组文章数量:1130349
I want to display list of custom taxonomies as a dropdown list.
I've found the solution here, @alexufo 's answer
Display a custom taxonomy as a dropdown on the edit posts page
But the problem with that is that when i publish the post with selected taxonomy it creates another taxonomy automatically.
I can't comment on his answer as i don't have 50 reputation.
here's my code
function realty_type() {
$args = array(
'show_ui' => true,
'meta_box_cb' => 'drop_cat',
);
register_taxonomy( 'realty_type', array( 'my-CPT' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'realty_type', 0 );
function drop_cat( $post, $box ) {
$defaults = array('taxonomy' => 'category');
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);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="acf-taxonomy-field categorydiv">
<?php
$name = ( $taxonomy == 'category' ) ? 'post_category' : 'tax_input[' . $taxonomy . ']';
echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
?>
<? $term_obj = wp_get_object_terms($post->ID, $taxonomy ); //_log($term_obj[0]->term_id)?>
<ul id="<?php echo $taxonomy; ?>checklist" data-wp-lists="list:<?php echo $taxonomy?>" class="categorychecklist form-no-clear">
<?php //wp_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy) ) ?>
</ul>
<?php wp_dropdown_categories( array( 'taxonomy' => $taxonomy, 'hide_empty' => 0, 'name' => "{$name}[]", 'selected' => $term_obj[0]->term_id, 'orderby' => 'name', 'hierarchical' => 0, 'show_option_none' => '—' ) ); ?>
</div>
<?php
}
I want to display list of custom taxonomies as a dropdown list.
I've found the solution here, @alexufo 's answer
Display a custom taxonomy as a dropdown on the edit posts page
But the problem with that is that when i publish the post with selected taxonomy it creates another taxonomy automatically.
I can't comment on his answer as i don't have 50 reputation.
here's my code
function realty_type() {
$args = array(
'show_ui' => true,
'meta_box_cb' => 'drop_cat',
);
register_taxonomy( 'realty_type', array( 'my-CPT' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'realty_type', 0 );
function drop_cat( $post, $box ) {
$defaults = array('taxonomy' => 'category');
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);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="acf-taxonomy-field categorydiv">
<?php
$name = ( $taxonomy == 'category' ) ? 'post_category' : 'tax_input[' . $taxonomy . ']';
echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
?>
<? $term_obj = wp_get_object_terms($post->ID, $taxonomy ); //_log($term_obj[0]->term_id)?>
<ul id="<?php echo $taxonomy; ?>checklist" data-wp-lists="list:<?php echo $taxonomy?>" class="categorychecklist form-no-clear">
<?php //wp_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy) ) ?>
</ul>
<?php wp_dropdown_categories( array( 'taxonomy' => $taxonomy, 'hide_empty' => 0, 'name' => "{$name}[]", 'selected' => $term_obj[0]->term_id, 'orderby' => 'name', 'hierarchical' => 0, 'show_option_none' => '—' ) ); ?>
</div>
<?php
}
Share
Improve this question
edited Sep 6, 2018 at 8:53
Hector
6821 gold badge7 silver badges18 bronze badges
asked Sep 5, 2018 at 23:43
Mais_CuleMais_Cule
552 silver badges7 bronze badges
2 Answers
Reset to default 3Below is the easiest solution I use for displaying custom taxonomy as the dropdown instead of checkboxes.
1/ Use third party WordPress plugin 'ACF' to create the taxonomy 'Relational' field type and display it as dropdown as shown in below screenshot.
https://wordpress/plugins/advanced-custom-fields/
For displaying taxonomy selected you can refer documentation of ACF for relational taxonomy field type.
https://www.advancedcustomfields/resources/taxonomy/
2/ Hide taxonomy checkbox by simply adding 'meta_box_cb' as 'false' when registering your custom taxonomy.
Hope this helps..!!
Finally I found the solution after some trials and error.
In the registering taxonomy arguments, I just had to set 'hierarchical' => true for it to stop incrementing taxonomy and keep adding taxonomy automatically.
I'm not sure as to why that was causing the problem after going through documents, If anyone knows the reason please explain.
function realty_type() {
$args = array(
'show_ui' => true,
'meta_box_cb' => 'drop_cat',
'hierarchical' => true
);
register_taxonomy( 'realty_type', array( 'YOUR_POST_TYPE' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'realty_type', 0 );
I want to display list of custom taxonomies as a dropdown list.
I've found the solution here, @alexufo 's answer
Display a custom taxonomy as a dropdown on the edit posts page
But the problem with that is that when i publish the post with selected taxonomy it creates another taxonomy automatically.
I can't comment on his answer as i don't have 50 reputation.
here's my code
function realty_type() {
$args = array(
'show_ui' => true,
'meta_box_cb' => 'drop_cat',
);
register_taxonomy( 'realty_type', array( 'my-CPT' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'realty_type', 0 );
function drop_cat( $post, $box ) {
$defaults = array('taxonomy' => 'category');
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);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="acf-taxonomy-field categorydiv">
<?php
$name = ( $taxonomy == 'category' ) ? 'post_category' : 'tax_input[' . $taxonomy . ']';
echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
?>
<? $term_obj = wp_get_object_terms($post->ID, $taxonomy ); //_log($term_obj[0]->term_id)?>
<ul id="<?php echo $taxonomy; ?>checklist" data-wp-lists="list:<?php echo $taxonomy?>" class="categorychecklist form-no-clear">
<?php //wp_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy) ) ?>
</ul>
<?php wp_dropdown_categories( array( 'taxonomy' => $taxonomy, 'hide_empty' => 0, 'name' => "{$name}[]", 'selected' => $term_obj[0]->term_id, 'orderby' => 'name', 'hierarchical' => 0, 'show_option_none' => '—' ) ); ?>
</div>
<?php
}
I want to display list of custom taxonomies as a dropdown list.
I've found the solution here, @alexufo 's answer
Display a custom taxonomy as a dropdown on the edit posts page
But the problem with that is that when i publish the post with selected taxonomy it creates another taxonomy automatically.
I can't comment on his answer as i don't have 50 reputation.
here's my code
function realty_type() {
$args = array(
'show_ui' => true,
'meta_box_cb' => 'drop_cat',
);
register_taxonomy( 'realty_type', array( 'my-CPT' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'realty_type', 0 );
function drop_cat( $post, $box ) {
$defaults = array('taxonomy' => 'category');
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);
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="acf-taxonomy-field categorydiv">
<?php
$name = ( $taxonomy == 'category' ) ? 'post_category' : 'tax_input[' . $taxonomy . ']';
echo "<input type='hidden' name='{$name}[]' value='0' />"; // Allows for an empty term set to be sent. 0 is an invalid Term ID and will be ignored by empty() checks.
?>
<? $term_obj = wp_get_object_terms($post->ID, $taxonomy ); //_log($term_obj[0]->term_id)?>
<ul id="<?php echo $taxonomy; ?>checklist" data-wp-lists="list:<?php echo $taxonomy?>" class="categorychecklist form-no-clear">
<?php //wp_terms_checklist($post->ID, array( 'taxonomy' => $taxonomy) ) ?>
</ul>
<?php wp_dropdown_categories( array( 'taxonomy' => $taxonomy, 'hide_empty' => 0, 'name' => "{$name}[]", 'selected' => $term_obj[0]->term_id, 'orderby' => 'name', 'hierarchical' => 0, 'show_option_none' => '—' ) ); ?>
</div>
<?php
}
Share
Improve this question
edited Sep 6, 2018 at 8:53
Hector
6821 gold badge7 silver badges18 bronze badges
asked Sep 5, 2018 at 23:43
Mais_CuleMais_Cule
552 silver badges7 bronze badges
2 Answers
Reset to default 3Below is the easiest solution I use for displaying custom taxonomy as the dropdown instead of checkboxes.
1/ Use third party WordPress plugin 'ACF' to create the taxonomy 'Relational' field type and display it as dropdown as shown in below screenshot.
https://wordpress/plugins/advanced-custom-fields/
For displaying taxonomy selected you can refer documentation of ACF for relational taxonomy field type.
https://www.advancedcustomfields/resources/taxonomy/
2/ Hide taxonomy checkbox by simply adding 'meta_box_cb' as 'false' when registering your custom taxonomy.
Hope this helps..!!
Finally I found the solution after some trials and error.
In the registering taxonomy arguments, I just had to set 'hierarchical' => true for it to stop incrementing taxonomy and keep adding taxonomy automatically.
I'm not sure as to why that was causing the problem after going through documents, If anyone knows the reason please explain.
function realty_type() {
$args = array(
'show_ui' => true,
'meta_box_cb' => 'drop_cat',
'hierarchical' => true
);
register_taxonomy( 'realty_type', array( 'YOUR_POST_TYPE' ), $args );
}
// Hook into the 'init' action
add_action( 'init', 'realty_type', 0 );
本文标签: pluginsDisplay custom taxonomy as dropdown list
版权声明:本文标题:plugins - Display custom taxonomy as dropdown list 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749029184a2305587.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论