AYoub Erradi
AYoub Erradi

Reputation: 21

I need this drop-down php code to redirect people to a specific taxonomy in WordPress

I change the add to cart.php button to this code that I wrote but it's not working i need a drop-down menu without submit button to make people search for the closet library from my taxonomy tags :

code

<?php
if ( is_front_page() ) {

    ?>
    <div class="chosir_mon_livre_home">
        <div class="col-md-5  chosir_mon_livre ">
            Cherchez <br> mon livre
        </div>
        <div class="col-md-7">
            <div class="styled-select-product blue slate" style="border: 2px solid #ececec;
        border-radius: 10px;
        width: 250px;
        float: right;
        margin-right: -1px;
        background: #ececec;">
                <select name="ville" class="select_choice_ville">
                    <option value="0" selected="selected" disabled="disabled">Sélectionner</option>
                    <?php
                    $categories = get_terms( [ 'taxonomy' => 'ville', 'hide_empty' => false ] );

                    foreach ( $categories as $cat ) {
                        ?>
                    <option value="<?= $cat->term_id; ?>">
                        <?= $cat->name; ?>
                    </option>
                    <?php
                    }

                    ?>
                </select>
            </div>
        </div>
    </div>


<?php  } ?>

Upvotes: 2

Views: 91

Answers (1)

Jurgen Oldenburg
Jurgen Oldenburg

Reputation: 179

You'll need a bit of javascript to do this. On change of the select you need to redirect to the archive url of the taxonomy.

I would add the taxonomy url to the option value attribute:

<option value="<?php echo get_term_link($cat); ?>">

Than add a little jquery to pick up the change event and redirect to the category:

$('select.select_choice_ville').on('change', function(){
    window.location.href = $this.val();
})

Upvotes: 1

Related Questions