You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress Ajax筛选问题:自定义代码在不同页面无法正确筛选文章

WordPress Ajax筛选问题:自定义代码在不同页面无法正确筛选文章

我看了你的代码,问题主要出在两个地方:一是Ajax处理函数里硬编码了Pre-Festival的分类,二是Ajax请求没有传递当前页面的CAT_ID参数,导致服务器不知道要基于哪个页面的基础分类来筛选。下面是具体的修复步骤:


1. 移除Ajax函数里的硬编码分类

你的events_filter_fn函数一开始就添加了一个固定的pre-festival-events-2023分类筛选,这会导致所有页面的筛选结果都强制包含这个分类。把这段硬编码的代码删掉:

// 删掉这段硬编码的筛选
$filter = array(
    array(
        'taxonomy' => 'portfolio_category', // Change this to your actual taxonomy name
        'field'    => 'slug',
        'terms'    => 'pre-festival-events-2023',
    )
);

替换成空数组,后续再根据参数添加筛选条件:

$filter = array();

2. 在Ajax请求中传递页面的CAT_ID

在短代码的JavaScript部分,需要把当前页面的CAT_ID(也就是短代码里的cat_id参数)传递给Ajax接口。修改getEventsResults函数:

function getEventsResults() {
    jQuery('#events-deck--content').html("Loading...");
    jQuery.get(
        '<?php echo esc_url(admin_url('admin-ajax.php')); ?>',
        {
            'action' : 'events_filter_fn',
            'category' : jQuery('#events-cat-filter-dropdown').val(),
            'tag' : jQuery('#events-tag-filter-dropdown').val(),
            'cat_id' : '<?php echo esc_attr($atts['cat_id']); ?>' // 新增这一行,传递当前页面的CAT_ID
        }
    ).done(function(response){
        console.log(response);
        if (response['success']) {
            // 修复这里:直接用response.data.output替换内容,不要重复append
            jQuery('#events-deck--content').html(response['data']['output']);
        }
    });
}

3. 在Ajax处理函数中使用传递的CAT_ID

现在要让服务器端的events_filter_fn函数接收并使用这个cat_id参数,把它作为基础筛选条件:

function events_filter_fn(){
    ob_start();

    $args = array(
        'posts_per_page' => -1,
        'post_type' => 'portfolio',
        'post_status'   => 'publish'
    );

    $filter = array();

    // 新增:添加当前页面的基础CAT_ID筛选
    if (isset($_GET['cat_id']) && !empty($_GET['cat_id'])) {
        $filter[] = array(
            'taxonomy' => 'portfolio_category',
            'field'    => 'term_id',
            'terms'    => $_GET['cat_id'],
        );
    }

    // 保留原有的分类和标签筛选逻辑
    if (isset($_GET['category']) && $_GET['category'] != 'all') {
        $filter[] = array(
            'taxonomy' => 'portfolio_category',
            'field'    => 'slug',
            'terms' => $_GET['category']
        );
    }

    if (isset($_GET['tag']) && $_GET['tag'] != 'all') {
        $filter[] = array(
            'taxonomy' => 'portfolio_tag',
            'field' => 'slug',
            'terms' => $_GET['tag']
        );
    }

    // 只有当有筛选条件时才添加tax_query
    if (!empty($filter)) {
        $args['tax_query'] = array(
            'relation' => 'AND',
            ...$filter
        );
    }

    $post_args = new WP_Query( $args );

    if ($post_args->have_posts()) :
        while ($post_args->have_posts()) : $post_args->the_post();
            get_template_part('template-parts/event', 'card');
        endwhile;
        wp_reset_postdata();
    else :
        echo 'No posts found';
    endif;

    echo wp_send_json_success(
        array(
            'output' => ob_get_clean(),
        )
    );
    wp_die();
}

这样修改之后,每个页面的筛选都会基于短代码里指定的CAT_ID,再加上用户选择的分类/标签,就能正确显示对应页面的内容了。

备注:内容来源于stack exchange,提问作者Hambs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 07:14:50