plugins - Display custom taxonomy as dropdown list

admin2025-06-04  46

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' => '&mdash;' ) ); ?>

    </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' => '&mdash;' ) ); ?>

    </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
Add a comment  | 

2 Answers 2

Reset to default 3

Below 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 );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749029208a315759.html

最新回复(0)