How to list categories and subcategories in select?

Asked

Viewed 82 times

-1

Personal I have 2 custom taxonomies in a post, are they, City and States. But I only want to use the taxonomy Cities and every city put her being Ascendant of their respective State. Once this is done I would like to make these 2 selects work as follows: first Select would give me the Status options and the Second Select the City options according to the selected State! I have these code below, but I don’t quite know how to do that I need. Here also is an image of how today is this select: https://prnt.sc/vz0r92

        <form action="<?php echo site_url() ?>/wp-admin/admin-ajax.php" method="POST" id="filter">

        <div class="row">

        <div class="col-md-3 estados">
                <?php
                $taxonomy = 'estados'; ?>
                <?php $terms = get_terms($taxonomy); ?>

                <select id="<?php echo $taxonomy; ?>" name="<?php echo $taxonomy; ?>" class="span2">
                    <?php if ($terms = get_terms(array('taxonomy' => 'estados'))) : ?>

                        <option data-title="0" value="0">Selecione um Estado</option>

                        <?php foreach ($terms as $term) : ?>
                            <option value="<?php echo $term->term_id; ?>"><?php echo ucfirst($term->name); ?></option>
                        <?php endforeach; ?>

                    <?php endif; ?>

                </select>
            </div>

            <div class="col-md-6 cidades">
                <?php
                $taxonomy = 'cidades'; ?>
                <?php $terms = get_terms($taxonomy); ?>

                <select id="<?php echo $taxonomy; ?>" name="<?php echo $taxonomy; ?>" class="span4">
                    <?php if ($terms = get_terms(array('taxonomy' => 'cidades'))) : ?>

                        <option data-title="0" value="0">Selecione uma Cidade</option>
                        
                        <?php foreach ($terms as $term) : ?>
                            <option data-title="<?php echo $term->term_name; ?>" value="<?php echo $term->term_id; ?>"><?php echo ucfirst($term->name); ?></option>
                        <?php endforeach; ?>

                    <?php endif; ?>

                </select>

            </div>


            <div class="col-md-3">
                <button type="submit" class="btn btn-busca">Buscar</button>
            </div>

        </div>


    </form>

1 answer

0

To check if the term is Voce parent you can check the Parent attribute. If it is 0 it means that it is a state, if not, city.

$locais = get_terms($taxonomy);

foreach($terms as $term) {
    if ($term->parent == 0) { 
      //estados
    }else {
     //cidades
    }
}
  • Right, but how can I relate the state to the city?

  • When creating your taxonomy, you must declare that it has hierarchy: register_taxonomy(&#xA; 'local', //taxonomy name&#xA; 'user', //object for which the taxonomy is created&#xA; array( //taxonomy details&#xA; 'public' => true,&#xA; 'rewrite' => array('slug' => 'instaladores', 'with_front' => false, 'hierarchical' => true),&#xA; 'hierarchical' => true,&#xA;... , declaring this, will appear the select for Voce to relate on the screen of creating a city , do not forget to fill the parent_item and parent_label inside the array of Labels when registering taxonomy

Browser other questions tagged

You are not signed in. Login or sign up in order to post.