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

WordPress自定义文章类型Rewrite规则失效及页面跳转问题求助

WordPress自定义文章类型问题解决方案

问题1:Rewrite规则不生效,teams页面仅显示页眉页脚

你的Rewrite规则未生效,核心原因是未注册自定义查询变量,WordPress会自动过滤未注册的查询参数,导致页面无法正确获取department参数,进而无法加载对应内容。此外需确保重写规则已完成刷新。

修复步骤:

  1. 注册自定义查询变量
    在functions.php中添加以下代码,让WordPress识别department参数:
function custom_query_vars($vars) {
    $vars[] = 'department';
    return $vars;
}
add_filter('query_vars', 'custom_query_vars');
  1. 刷新重写规则
  • 登录WordPress后台,进入「设置」→「固定链接」
  • 无需修改任何设置,直接点击「保存更改」,强制刷新重写规则缓存
  1. 检查页面模板
    确认teams页面使用的模板(如page-teams.php)中,the_content()函数正常调用,确保短代码能被渲染输出。

问题2:员工筛选短代码无法正常展示结果

你的短代码存在几处逻辑缺陷,导致无法正确匹配并展示员工数据:

修复点:

  1. 替换get_page_by_title为slug查询
    get_page_by_title依赖完全匹配的标题,一旦部门标题有大小写差异或特殊字符就会失效。改用slug查询更可靠,因为URL参数是finance这类slug格式:
// 替换原get_page_by_title部分
$department_slug = isset($_GET['department']) ? sanitize_text_field($_GET['department']) : '';
$department = get_posts(array(
    'post_type' => 'department',
    'name' => $department_slug,
    'posts_per_page' => 1
));
$department = !empty($department) ? $department[0] : false;
  1. 修正meta_query的匹配规则
    如果Staff的department字段是ACF关联的部门(存储部门ID),compare应使用=而非LIKE,避免不必要的模糊匹配:
'meta_query' => array(
    array(
        'key' => 'department',
        'value' => $department->ID,
        'compare' => '='
    )
)
  1. 统一参数优先级逻辑
    短代码同时使用了短代码属性和$_GET参数,容易混淆。调整为优先取$_GET参数,无参数时使用短代码属性:
$atts = shortcode_atts(array(
    'department' => ''
), $atts);
$department_slug = isset($_GET['department']) ? sanitize_text_field($_GET['department']) : $atts['department'];

完整修正后的短代码:

function staff_filter_shortcode($atts) {
    $atts = shortcode_atts(array(
        'department' => ''
    ), $atts);

    // 优先获取GET参数,其次用短代码属性
    $department_slug = isset($_GET['department']) ? sanitize_text_field($_GET['department']) : $atts['department'];
    $department_name = ucwords(str_replace('-', ' ', $department_slug));

    $output = '<div class="staff-filter">';

    if ($department_slug) {
        // 通过slug查询部门,比标题更可靠
        $department = get_posts(array(
            'post_type' => 'department',
            'name' => $department_slug,
            'posts_per_page' => 1
        ));
        $department = !empty($department) ? $department[0] : false;

        if ($department) {
            $args = array(
                'post_type' => 'staff',
                'meta_query' => array(
                    array(
                        'key' => 'department',
                        'value' => $department->ID,
                        'compare' => '='
                    )
                )
            );

            $staff_query = new WP_Query($args);

            if ($staff_query->have_posts()) {
                $output .= '<h1>Staff in ' . esc_html($department->post_title) . ' Department</h1>';
                $output .= '<div class="staff-list">';
                while ($staff_query->have_posts()) {
                    $staff_query->the_post();
                    $role = get_field('role');
                    $output .= '<div class="staff-member">';
                    $output .= '<h2>' . get_the_title() . '</h2>';
                    if ($role) {
                        $output .= '<p><strong>Role:</strong> ' . esc_html($role) . '</p>';
                    }
                    $output .= '<div class="staff-content">' . get_the_content() . '</div>';
                    $output .= '</div>';
                }
                $output .= '</div>';
            } else {
                $output .= '<p>No staff members found for this department.</p>';
            }

            wp_reset_postdata();
        } else {
            $output .= '<p>Department not found.</p>';
        }
    } else {
        $output .= '<p>No department specified.</p>';
    }

    $output .= '</div>';

    return $output;
}
add_shortcode('staff_filter', 'staff_filter_shortcode');

内容的提问来源于stack exchange,提问作者Udendu Abasili

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 17:15:19