下拉过滤器无法正常工作

时间:2020-06-21 08:20:14

标签: php wordpress filter

我的代码:

<?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 :(得分:0)

   <?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;
    }
?>

尝试一下。

相关问题