user4042721
user4042721

Reputation:

Hierarchical taxonomy dropdown wordpress

I have some custom taxonomies built in theme, and on the taxonomy dropdown, taxonomies are not showed in hierarchy. It just show a dropdown list of all taxonomies, but not in hierarchical order.

This is what I've got:

register_taxonomy(
        'recipesets',
        'recipe',
        array(
            'public'=>true,
            'hierarchical' => true,
            'labels'=> $labels,
            'query_var' => 'recipesets',
            'show_ui' => true,
            'rewrite' => array( 'slug' => 'recipesets', 'with_front' => false ),
        )
    );                    
} 

and calling:

<label for="edit-title" class="control-label"><i class="fa fa-folder-o"></i><?php _e('Category:', 'agrg') ?></label>
                        <select name="cat" id="cat" class="postform">
                            <?php

                            $terms = get_terms("recipesets", "orderby=count&hide_empty=0");
                            if ( !is_wp_error( $terms ) ) {
                                foreach ( $terms as $term ) {
                                   echo "<option value='" . $term->name . "'>" . $term->name . "</option>";

                                }
                            }

                            ?>
                        </select>

What to do?

Upvotes: 1

Views: 5709

Answers (2)

Starjumper Tech SL
Starjumper Tech SL

Reputation: 416

Why not use the Wordpress function wp_dropdown_categories() which does all the work for you

$tax_args = array(
  'taxonomy'     => 'destination',
  'orderby'      => 'name',
  'show_count'   => 1,
  'hierarchical' => 1,
);
wp_dropdown_categories($tax_args);

Upvotes: 2

thaikolja
thaikolja

Reputation: 431

This is how I would do it:

<?php
    /** The taxonomy we want to parse */
    $taxonomy = "category";
    /** Get all taxonomy terms */
    $terms = get_terms($taxonomy, array(
            "orderby"    => "count",
            "hide_empty" => false
        )
    );
    /** Get terms that have children */
    $hierarchy = _get_term_hierarchy($taxonomy);
    ?>
    <select name="terms" id="terms">
        <?php
            /** Loop through every term */
            foreach($terms as $term) {
                /** Skip term if it has children */
                if($term->parent) {
                    continue;
                }
                echo '<option value="' . $term->name . '">' . $term->name . '</option>';
                /** If the term has children... */
                if($hierarchy[$term->term_id]) {
                    /** ...display them */
                    foreach($hierarchy[$term->term_id] as $child) {
                        /** Get the term object by its ID */
                        $child = get_term($child, "category");
                        echo '<option value="' . $term->name . '"> - ' . $child->name . '</option>';
                    }
                }
            }
        ?>
    </select>

Upvotes: 1

Related Questions