在清除过滤器按钮上单击时,将所有下拉列表设置为php中的默认值

时间:2019-05-24 09:27:58

标签: php wordpress dropdown

我是PHP和WordPress的新手。我正在使用2个下拉列表,一个用于过滤器类别,另一个用于按目的排序。对于排序和过滤,我没有使用任何钩子。以下代码用于过滤和排序方式实现,以及我用于下拉菜单的另一个代码段:

if($categoryFilter && $categoryFilter == 'all' && $searchKey == '' ) {

    $order = 'DESC';
    if($sortbyFilter == 'best_match') $order = 'ASC';

    $args = array(
        'post_type' => array('auction-detail','lot','asset'),
        'post_status' => 'publish',
        'orderby'     => 'date', 
        'order' => $order,
        'posts_per_page' => 10,
        'paged' => $paged
    );
    query_posts( $args );
}

if($categoryFilter && $categoryFilter != 'all' && $searchKey == '' ) {
    $sortbyFilter = 'newly_listed';
    $_POST['sortby-filter'] = $sortbyFilter;
    $args = array(
        'post_type' => array('lot','asset'),
        'post_status' => 'publish',
        //'orderby'     => 'post_title', 
        //'order' => 'ASC',
        'posts_per_page' => 10,
        'paged' => $paged,
        'tax_query' => array(
            array(
                'field' => 'slug',
                'terms' => $categoryFilter,
                'taxonomy'  => 'category'
            )
        )
    );
    query_posts( $args );
}



<div class="filterCols category">
<select id="selcategories" name="category" onchange="handleChange()">
<option value='all' <?php echo (isset($categoryFilter) && $categoryFilter 
== 'all') ? 'selected="selected"' : ''; ?>>All</option>
<option value='commercial_trucks' <?php echo (isset($categoryFilter) && 
$categoryFilter == 'commercial_trucks') ? 'selected="selected"' : ''; ? 
>>Heavy Machinary</option>
<option value='farm_machinery_implements' <?php echo 
(isset($categoryFilter) && $categoryFilter == 
'farm_machinery_implements') ? 'selected="selected"' : ''; ?>>Farm 
Equipment</option>
<option value='na' <?php echo (isset($categoryFilter) && $categoryFilter 
== 'na') ? 'selected="selected"' : ''; ?>>Construction Equipment</option>
</select>
<input hidden name="category-filter" id="category-filter" type="text" 
value="<?php echo $_SESSION['category']; ?>" />
</div>


Below is clear filter div :

<div class="filterCols sortby clearBtnWrapper">
<i class="fa fa-refresh" ></i><a href="#">Clear All</a></div>

我已经尝试过remove_all_filters(),但是由于没有钩子,所以运气不佳。有什么办法吗?

1 个答案:

答案 0 :(得分:1)

我没有看到“清除过滤器”按钮,也没有看到问题中的第二个选择,但是如何使用类似的东西:

<button onclick="clearFilters()">CLEAR FILTERS</button>

然后在您的.js或内联脚本中

function clearFilters(){
    jQuery('#selcategories').val('all');
    jQuery('#sortbyFilter').val('all');
}