要了解如何操作,首先需要知道url查询是如何工作的,或者更具体地说,需要什么查询变量,因此:
\'post_type\'
- 按PostType筛选\'area\'
- 按区域分类法过滤\'university\'
- 按大学分类法筛选
因此您的url如下所示:
// http://www.domain.com/?post_type=properties&area=area_term&university=university_term
现在我们知道了,我们使用所需的字段创建表单:
function display_area_university_filter_form(){
?>
<form method="post" action="">
<p><label for="area_t">Area: </label> <select name="area_t"><option value="">Select Area:</option>
<?php
$area_terms = get_categories(\'taxonomy=area\');
if (isset($_POST[\'area_t\'])){
foreach ($area_terms as $term) {
$option = \'<option value="\'.$term->slug.\'"\';
if ($_POST[\'area_t\'] == $term->slug){
$option.= \' selected="selected"\';
}
$option .= \'>\'.$term->name;
$option .= \'</option>\';
echo $option;
}
}else{
foreach ($area_terms as $term) {
$option = \'<option value="\'.$term->slug.\'"\';
$option .= \'>\'.$term->name;
$option .= \'</option>\';
echo $option;
}
}
?>
</select>
</p>
<p><label for="area_t">University: </label> <select name="area_t"><option value="">Select University:</option>
<?php
$uni_terms = get_categories(\'taxonomy=university\');
if (isset($_POST[\'univer\'])){
foreach ($uni_terms as $term) {
$option = \'<option value="\'.$term->slug.\'"\';
if ($_POST[\'univer\'] == $term->slug){
$option.= \' selected="selected"\';
}
$option .= \'>\'.$term->name;
$option .= \'</option>\';
echo $option;
}
}else{
foreach ($uni_terms as $term) {
$option = \'<option value="\'.$term->slug.\'"\';
$option .= \'>\'.$term->name;
$option .= \'</option>\';
echo $option;
}
}
?>
</select>
</p>
<input type="hidden" name="custom_filter" value="area_uni">
<p><input type="submit" name="submit" value="submit"></p>
</form>
<?php }
那么剩下的就是处理它:
if (isset($_POST[\'submit\']) && isset($_POST[\'custom_filter\']) && $_POST[\'custom_filter\'] == "area_uni"){
$url = get_bloginfo(\'url\');
$area = $uni = \'\';
if (isset($_POST[\'area_t\'])){
$area = \'&area=\'.$_POST[\'area_t\'];
}
if (isset($_POST[\'univer\'])){
$uni = \'&university=\'.$_POST[\'univer\'];
}
$url .= \'/?post_type=properties\'.$area.$uni;
wp_redirect( $url );
exit();
}