WordPress

formularze — warunkowy 3-poziomowy filtr rozwijany dla niestandardowego typu postu

  • 7 marca, 2023
  • 4 min read
formularze — warunkowy 3-poziomowy filtr rozwijany dla niestandardowego typu postu


Potrzebuję pomocy w następujących kwestiach: Mam niestandardowy typ postu z hierarchiczną taksonomią o nazwie lokalizacja. Kraj jest rodzicem stanu, który jest rodzicem miasta (Kraj -> stan -> miasto). Próbuję utworzyć 3 listy rozwijane:

  1. Wybierz kraj (najwyższy poziom)
  2. Następnie można wybrać stany będące dziećmi tego kraju
  3. Na koniec można następnie wybrać miasta, które są dziećmi wybranego stanu

Po wybraniu miasta użytkownik zostaje przekierowany na stronę archiwum tego terminu miasta: example.com/location/selected-city.

Znalazłem następujący zasób: Warunkowy dwupoziomowy filtr rozwijany dla niestandardowego typu postu

I użyłem go do stworzenia poniższego kodu. Ale jedną rzeczą jest to, że po wybraniu stanu i przesłaniu formularza stan, który wyświetla, resetuje się do pierwszej opcji, podczas gdy wybrana wartość stanu jest pobierana przez listę rozwijaną miasta. Wreszcie, po wybraniu miasta, w jaki sposób mogę przekazać ślimak tego terminu do adresu URL, aby wynik końcowy przekierowywał na stronę example.com/location/selected-city ?

<form method="POST" action="">
        <div>
        <?php
                $args = array(
                    // hierarchical is needed to define depth
                    'hierarchical' => 1,
                    // regions are the top level in a hierarchical taxonomy
                    'depth' => 1,
                    'term_taxonomy_id'  => array( 82, 83),
                    'orderby' => 'name',
                    // we're not echoing, because we want to construct a no button solution
                    'echo' => 0,
                    'taxonomy' => 'location',
                    // this leads to variable name $_POST['region']
                    'name' => 'country'
                );
                if( ! isset($_POST['country']) ):
                    // if no region was selected prior we show this by default
                    $args['show_option_none'] = 'Select Country';
                else:
                    // otherwise make sure the region form shows what was selected before
                    $args['selected'] = $_POST['country'];
                endif;
                // we're putting the dropdown output into a variable
                $country = wp_dropdown_categories( $args );
                // this enables the buttonless js possibility
                $country = preg_replace("#<select([^>]*)>#", "<select$1 onchange="return this.form.submit()">", $country);
                // now echo the dropdown output
                echo $country;
                // the »<noscript>...</noscript> part makes sure there is a fallback in case there is no js
            ?>
            <noscript>
                <div>
                    <input type="submit" value="country" />
                </div>
            </noscript>
        </div>
    </form>


    <?php
        // the state dropdown is only shown if a country was selected
        if( isset($_POST['country']) && $_POST['country'] ):
    ?>
        <form method="POST" action="">
            <?php // we add a hidden input to hand over the country selected ?>
            <input type="hidden" name="country" value="<?php echo $_POST['country'] ?>">
            <div>
                <?php
                    $args = array(
                        // the states to show are children of the prior selected country
                        'parent' => $_POST['country'],
                        'hide_if_empty' => true,
                        'orderby' => 'name',
                        'echo' => 0,
                        'taxonomy' => 'location',
                        'name' => 'state'
                    );
                    if( ! isset($_POST['state']) ):
                        $args['show_option_none'] = 'Select State';
                    endif;
                    $state = wp_dropdown_categories( $args );
                    $state = preg_replace("#<select([^>]*)>#", "<select$1 onchange="return this.form.submit()">", $state);
                    echo $state;
                ?>
                <noscript>
                    <div>
                        <input type="submit" value="state" />
                    </div>
                </noscript>
            </div>
        </form>
    <?php endif; ?>


<?php
    // the area dropdown is only shown if a state was selected
    if( isset($_POST['country']) && $_POST['country'] && isset($_POST['state']) && $_POST['state'] ):
?>
    <form method="POST" action="">
        <?php // we add a hidden input to hand over the state selected ?>
        <input type="hidden" name="state" value="<?php echo $_POST['state'] ?>">
        <div>
            <?php
                $args = array(
                    // the states to show are children of the prior selected state
                    'parent' => $_POST['state'],
                    'hide_if_empty' => true,
                    'orderby' => 'name',
                    'echo' => 0,
                    'taxonomy' => 'location',
                    'name' => 'city'
                );
                if( ! isset($_POST['city']) ):
                    $args['show_option_none'] = 'Select City';
                endif;
                $city = wp_dropdown_categories( $args );
                $city = preg_replace("#<select([^>]*)>#", "<select$1 onchange="return this.form.submit()">", $city);
                echo $city;
            ?>
            <noscript>
                <div>
                    <input type="submit" value="city" />
                </div>
            </noscript>
        </div>
    </form>
<?php endif; ?>


Źródło

Warto przeczytać!  paypal — Jak mogę skonfigurować LINE Pay jako metodę płatności w mojej witrynie WordPress połączonej z moim kontem bankowym?