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

如何重写WordPress主题已注册的自定义分类法URL?

自定义分类法URL重写实现方案(两种目标格式)

没问题,你想要的两种URL结构都可以通过WordPress的重写规则体系实现,我分两种情况给你详细说明具体步骤:

方案一:domain.com/doctor/dentist/peje/(位置放在路径末尾)

这个方案更贴合WordPress默认的分类/归档URL逻辑,实现步骤如下:

  1. 添加自定义重写规则
    在主题的functions.php文件或者自定义插件中添加以下代码,用来匹配末尾带位置的URL:

    function custom_llocs_suffix_rewrite() {
        // 匹配固定结构 /doctor/dentist/[位置别名]/
        add_rewrite_rule(
            '^doctor/dentist/([^/]+)/?$',
            'index.php?post_type=doctor&taxonomy=dentist&term=dentist&llocs=$matches[1]',
            'top'
        );
        // 如果dentist是动态分类(比如有多个医生分类),可以用下面的动态规则替代上面的:
        // add_rewrite_rule(
        //     '^doctor/([^/]+)/([^/]+)/?$',
        //     'index.php?post_type=doctor&taxonomy=dentist&term=$matches[1]&llocs=$matches[2]',
        //     'top'
        // );
    }
    add_action('init', 'custom_llocs_suffix_rewrite');
    
  2. 注册查询变量
    让WordPress能够识别llocs这个自定义参数,添加以下过滤代码:

    function custom_add_llocs_query_var($vars) {
        $vars[] = 'llocs';
        return $vars;
    }
    add_filter('query_vars', 'custom_add_llocs_query_var');
    
  3. 调整查询过滤内容
    当访问重写后的URL时,让WordPress自动根据位置参数过滤医生列表,添加:

    function filter_doctors_by_location($query) {
        if (!is_admin() && $query->is_main_query() && is_post_type_archive('doctor')) {
            $location_slug = get_query_var('llocs');
            if ($location_slug) {
                $query->set('tax_query', array(
                    array(
                        'taxonomy' => 'llocs',
                        'field'    => 'slug',
                        'terms'    => $location_slug,
                    )
                ));
            }
        }
    }
    add_action('pre_get_posts', 'filter_doctors_by_location');
    
  4. 刷新重写规则
    登录WordPress后台,进入「设置」→「固定链接」,随便修改一下设置(比如切换到另一种格式再切回来),然后点击「保存更改」,这样新的重写规则就会生效。


方案二:domain.com/peje/doctor/dentist/(位置放在路径开头)

这个结构需要把位置作为前缀,实现逻辑和方案一类似,只是重写规则的正则不同:

  1. 添加前缀式重写规则
    替换方案一中的重写规则代码为:

    function custom_llocs_prefix_rewrite() {
        // 匹配前缀结构 /[位置别名]/doctor/dentist/
        add_rewrite_rule(
            '^([^/]+)/doctor/dentist/?$',
            'index.php?post_type=doctor&taxonomy=dentist&term=dentist&llocs=$matches[1]',
            'top'
        );
        // 动态分类版本:
        // add_rewrite_rule(
        //     '^([^/]+)/doctor/([^/]+)/?$',
        //     'index.php?post_type=doctor&taxonomy=dentist&term=$matches[2]&llocs=$matches[1]',
        //     'top'
        // );
    }
    add_action('init', 'custom_llocs_prefix_rewrite');
    
  2. 其余步骤和方案一一致
    同样需要注册llocs查询变量、添加查询过滤逻辑,最后刷新固定链接规则。


注意事项

  • 如果你的doctor不是自定义文章类型,而是分类法,请根据实际情况调整index.php中的查询参数(比如把post_type=doctor改成category_name=doctor)。
  • 测试时如果URL跳转404,大概率是没刷新重写规则,再去固定链接页面保存一次即可。
  • 所有代码建议放在子主题的functions.php中,避免主题更新后代码丢失。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 13:17:39