筛选功能

2023-12-25 1次

自定义分类法

function filterTaxonomy($filter_name, $title_name)
{
    //var_dump($filter_name);
    if (empty($filter_name)) {
        return false;
    }
    $taxArgs = array('taxonomy' => $filter_name);
    // var_dump($taxArgs);
    $taxTerms = get_terms($filter_name, $taxArgs);
    $count = count($taxTerms);
    // echo "<pre>";
    // var_dump($taxTerms);
    if ($count > 0) {
        $is_all =  get_query_var($filter_name) ? '' : 'on';
        $all = esc_url(preg_replace('/\/page\/\d+/', '', remove_query_arg($filter_name))); ?>
        <div class="product-filter">
            <div class="product-filter-title">
                <h3 class="tfont-lv3"><?php echo $title_name; ?></h3>
            </div>
            <div class="product-filter-lists">
                <ul>
                    <li>
                        <a href="<?php echo $all; ?>#productsign" class="<?php echo $is_all; ?>">全部</a>
                    </li>
                    <?php
                    foreach ($taxTerms as $key => $value) {
                        $is_checked = (get_query_var($filter_name) == $value->term_id) ? 'on' : '';
                        $url = add_query_arg(array($filter_name => $value->term_id, "paged" => false));
                    ?>
                        <li>
                            <a href="<?php echo esc_url(preg_replace('/\/page\/\d+/', "", $url)); ?>#productsign" class="<?php echo $is_checked; ?>"><?php echo $value->name; ?></a>
                        </li>
                    <?php } ?>
                </ul>
            </div>
        </div>
    <?php }
}

自定义字段

function filterMetaArticle($filter_name, $title_name)
{
    $fields = get_field_object($filter_name);
    $choices = $fields['choices'];
    $param = $_GET[$filter_name];
    // var_dump($choices);
    if ($choices) {
        $is_all =  $param ? '' : 'selected';
        $all = preg_replace('/\/page\/\d+/', '', remove_query_arg($filter_name)); ?>
        <div class="jobs-filter-item jobs-filter-active">
            <h3 class="font-Guardian-blod title-level-3"><?php echo $title_name; ?><i class="iconfont"></i></h3>
            <div class="jobs-filter-lists">
                <ul>
                    <li class="<?php echo $is_all; ?>"><a href="<?php echo $all; ?>#career-miao">all</a></li>
                    <?php foreach ($choices as $key => $value) {
                        $is_checked = ($param == $key) ? 'selected' : '';
                        $url = preg_replace('/\/page\/\d+/', '', add_query_arg(array($filter_name => $key))); ?>
                        <li class="<?php echo $is_checked; ?>"><a href="<?php echo $url; ?>#career-miao"><?php echo $value; ?></a></li>
                    <?php } ?>
                </ul>
            </div>
        </div>
    <?php }
}

模板调用

$taxArray = array(
    'relation' => 'AND'
);
$terms_id = get_query_var('filter_pro_type');
if (!empty($terms_id)) {
    $array_temp = array(
        'taxonomy' => 'filter_pro_type',
        'field' => 'id',
        'terms' => $terms_id,
    );
    array_push($taxArray, $array_temp);
}
$terms_id = get_query_var('filter_pro_qa');
if (!empty($terms_id)) {
    $array_temp = array(
        'taxonomy' => 'filter_pro_qa',
        'field' => 'id',
        'terms' => $terms_id,
    );
    array_push($taxArray, $array_temp);
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
//
$args = array(
    'cat' => $cat,
    'post_type' => 'post',
    'posts_per_page' => 12,
    'paged' => $paged,
    'tax_query' => $taxArray
);
//echo "<pre>";
//print_r($args);
$wp_query = new WP_Query($args);
Prev
Next