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

WordPress固定链接添加blog与分类名配置问题求助

WordPress固定链接404问题解决方法

问题根源

你当前的配置导致路径冲突:WordPress无法区分/blog/politics/是分类页还是文章的分类路径段,默认优先匹配分类规则,所以访问文章时会返回404。

分步解决方案

1. 重置基础固定链接设置

  • 进入后台「设置」→「固定链接」
  • 固定链接结构设置为:/blog/%category%/%postname%/
  • 分类前缀设置为空(删除当前填写的blog)

2. 添加自定义重写规则

在主题的functions.php文件(或自定义代码插件)中添加以下代码,强制分类页使用/blog/[分类slug]/结构,并让文章链接规则优先匹配:

// 修改分类permalink结构
function custom_category_permalink($permalink, $term, $taxonomy) {
    if ($taxonomy === 'category') {
        return '/blog/' . $term->slug . '/';
    }
    return $permalink;
}
add_filter('term_link', 'custom_category_permalink', 10, 3);

// 添加分类重写规则
function custom_category_rewrite() {
    add_rewrite_rule(
        'blog/([^/]+)/?$',
        'index.php?category_name=$matches[1]',
        'top'
    );
}
add_action('init', 'custom_category_rewrite');

// 添加文章重写规则(优先级高于分类)
function custom_post_rewrite() {
    add_rewrite_rule(
        'blog/([^/]+)/([^/]+)/?$',
        'index.php?category_name=$matches[1]&name=$matches[2]',
        'top'
    );
}
add_action('init', 'custom_post_rewrite');

3. 刷新重写规则

回到「固定链接」设置页面,无需修改任何内容,直接点击「保存更改」,让WordPress更新重写规则。

验证结果

完成后即可得到你想要的链接结构:

  • 博客首页:domain.com/blog/
  • 分类页:domain.com/blog/politics/
  • 文章页:domain.com/blog/politics/who-is-donald-trump/

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.05 23:54:25