在我的搜索中。php页面,我有一个“排序方式”下拉列表,它几乎完全符合我的要求--
<select class="dropdown-class" name="sort-posts" id="sortbox" onchange="document.location.href=location.href+this.options[this.selectedIndex].value;">
<option disabled>Sort by</option>
<option value="&orderby=date&order=dsc">Newest</option>
<option value="&orderby=date&order=asc">Oldest</option>
</select>
<script type="text/javascript">
<?php if (( $_GET[\'orderby\'] == \'date\') && ( $_GET[\'order\'] == \'dsc\')) { ?>
document.getElementById(\'sortbox\').value=\'orderby=date&order=dsc\';
<?php } elseif (( $_GET[\'orderby\'] == \'date\') && ( $_GET[\'order\'] == \'asc\')) { ?>
document.getElementById(\'sortbox\').value=\'orderby=date&order=asc\';
<?php } else { ?>
document.getElementById(\'sortbox\').value=\'orderby=date&order=desc\';
<?php } ?>
</script>
当Im出现在搜索结果页面上时,排序下拉列表将根据日期对当前结果进行排序,方法是抓取url并附加到url,然后用结果重新加载页面---// Before
mydomain.com/?s=Search&property_city=new-york-city&beds=Any
// After
mydomain.com/?s=Search&property_city=new-york-city&beds=Any&orderby=date&order=dsc
然而,我现在正试图进一步改进代码,使用它根据数字自定义字段(从高到低和从低到高)进行排序。似乎我能找到的关于这个主题的所有信息都有更复杂的方法。是否有必要使用我已经开始使用的代码来执行此操作?
UPDATE
我好像越来越近了-在我的搜索中。php页面我的循环之前有这个----
<select class="dropdown-class" name="sort-posts" id="sortbox" onchange="document.location.href=location.href+this.options[this.selectedIndex].value;">
<option disabled>Sort by</option>
<option value="&orderby=date&order=dsc">Newest</option>
<option value="&orderby=date&order=asc">Oldest</option>
<option value="&orderby=property_price&order=asc">Most Expensive</option>
<option value="&orderby=property_price&order=dsc">Least Expensive</option>
<option value="&orderby=property_area&order=dsc">Largest</option>
<option value="&orderby=property_area&order=asc">Smallest</option>
</select>
<script type="text/javascript">
<?php if (( $_GET[\'orderby\'] == \'date\') && ( $_GET[\'order\'] == \'dsc\')) { ?>
document.getElementById(\'sortbox\').value=\'orderby=date&order=dsc\';
<?php } elseif (( $_GET[\'orderby\'] == \'date\') && ( $_GET[\'order\'] == \'asc\')) { ?>
document.getElementById(\'sortbox\').value=\'orderby=date&order=asc\';
<?php } elseif (( $_GET[\'orderby\'] == \'property_price\') && ( $_GET[\'order\'] == \'asc\')) { ?>
document.getElementById(\'sortbox\').value=\'orderby=property_price&order=asc\';
<?php } elseif (( $_GET[\'orderby\'] == \'property_price\') && ( $_GET[\'order\'] == \'dsc\')) { ?>
document.getElementById(\'sortbox\').value=\'orderby=property_price&order=dsc\';
<?php } elseif (( $_GET[\'orderby\'] == \'property_area\') && ( $_GET[\'order\'] == \'asc\')) { ?>
document.getElementById(\'sortbox\').value=\'orderby=property_area&order=asc\';
<?php } elseif (( $_GET[\'orderby\'] == \'property_area\') && ( $_GET[\'order\'] == \'dsc\')) { ?>
document.getElementById(\'sortbox\').value=\'orderby=property_area&order=dsc\';
<?php } else { ?>
document.getElementById(\'sortbox\').value=\'orderby=date&order=desc\';
<?php } ?>
</script>
其中显示“orderby=xxx”,我只使用我的名称作为自定义字段。