Выпадающий фильтр не работает должным образом - PullRequest
0 голосов
/ 21 июня 2020

Мой код:

<?php
    function filter_profiles_by_country()
    {
        $url = get_site_url();
        if ( $terms = get_terms( array('taxonomy' => 'country','orderby' => 'name') ) )
        {
            // if categories exist, display the dropdown
            echo '<select name="categoryfilter" onchange="if (this.value) window.location.href=this.value">';
            echo '    <option value="'.$url.'/profiles">All Profiles...</option>';
            foreach ( $terms as $term )
            {
                // ID of the category as an option value
                echo '    <option value="'.$url ."/country/". $term->name . '">' . $term->name . '</option>'; 
            }
            echo '</select>';
        }
    }
?>

Когда я нажимаю «Все профили», я перехожу на страницу / profiles /. Но не работает.

1 Ответ

0 голосов
/ 27 июня 2020
   <?php 

function filter_profiles_by_country(){
        $url = get_site_url();
        global $wp;
    $current_url = home_url(add_query_arg(array(), $wp->request));
    
    if( $terms = get_terms( array(
        'taxonomy' => 'country',
        'orderby' => 'name'
    ) ) ) : 
        // if categories exist, display the dropdown
        echo '<select name="categoryfilter" onchange="if (this.value) window.location.href=this.value"><option value="'.$url.'/profiles/">All Profiles...</option>';
        foreach ( $terms as $term ) :
            $loadedItem = $url."/country/".$term->name;
            $selectedItem = ($current_url == $loadedItem)? "selected": "";
            echo '<option '.$selectedItem.' value="'.$url ."/country/". $term->name . '">' . $term->name . '</option>'; // ID of the category as an option value
        endforeach;
        echo '</select>';
    endif;
    }
?>

Попробуйте это.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...