下拉过滤器无法正常工作

我的代码:


<?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/ 页面。但它不起作用。


萧十郎
浏览 120回答 1
1回答

慕丝7291255

&nbsp; <?php&nbsp;function filter_profiles_by_country(){&nbsp; &nbsp; &nbsp; &nbsp; $url = get_site_url();&nbsp; &nbsp; &nbsp; &nbsp; global $wp;&nbsp; &nbsp; $current_url = home_url(add_query_arg(array(), $wp->request));&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if( $terms = get_terms( array(&nbsp; &nbsp; &nbsp; &nbsp; 'taxonomy' => 'country',&nbsp; &nbsp; &nbsp; &nbsp; 'orderby' => 'name'&nbsp; &nbsp; ) ) ) :&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // if categories exist, display the dropdown&nbsp; &nbsp; &nbsp; &nbsp; echo '<select name="categoryfilter" onchange="if (this.value) window.location.href=this.value"><option value="'.$url.'/profiles/">All Profiles...</option>';&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $terms as $term ) :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $loadedItem = $url."/country/".$term->name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $selectedItem = ($current_url == $loadedItem)? "selected": "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<option '.$selectedItem.' value="'.$url ."/country/". $term->name . '">' . $term->name . '</option>'; // ID of the category as an option value&nbsp; &nbsp; &nbsp; &nbsp; endforeach;&nbsp; &nbsp; &nbsp; &nbsp; echo '</select>';&nbsp; &nbsp; endif;&nbsp; &nbsp; }?>尝试这个。
打开App,查看更多内容
随时随地看视频慕课网APP