---
title: "WordPress 纯代码去掉 URL 的 Category 分类前缀 - 一纸忘忧"
description: "折腾 WordPress 的小伙伴们都知道，一些没有集成去除 /category/ 功能的主题，在每次修改固定链接后，分类链接中会出现一个 /category/ 目录。如果想要去掉分类链接中的 /category/ 目录，可以使用插件。网上也有许多插件可以直接去除（其实很多主题已经内置了这个功能）。但是，今天带来的是纯"
canonical_url: "https://www.ikxin.com/article/wordpress-remove-category"
last_updated: "2026-09-16T09:59:27.259Z"
locale: zh-cn
meta:
  "og:description": "折腾 WordPress 的小伙伴们都知道，一些没有集成去除 /category/ 功能的主题，在每次修改固定链接后，分类链接中会出现一个 /category/ 目录。如果想要去掉分类链接中的 /category/ 目录，可以使用插件。网上也有许多插件可以直接去除（其实很多主题已经内置了这个功能）。但是，今天带来的是纯"
  "og:image": "https://www.ikxin.com/_og/d/h_630%2Ca_~V29yZFByZXNzIOe6r-S7o-eggeWOu-aOiSBVUkwg55qEIENhdGVnb3J5IOWIhuexu-WJjee8gCAtIOS4gOe6uOW~mOW~pw%2Cc_Article%2Ctitle_~V29yZFByZXNzIOe6r-S7o-eggeWOu-aOiSBVUkwg55qEIENhdGVnb3J5IOWIhuexu-WJjee8gA%2Cdescription_~5oqY6IW-IFdvcmRQcmVzcyDnmoTlsI~kvJnkvLTku6zpg73nn6XpgZPvvIzkuIDkupvmsqHmnInpm4bmiJDljrvpmaQgL2NhdGVnb3J5LyDlip~og73nmoTkuLvpopjvvIzlnKjmr4~mrKHkv67mlLnlm7rlrprpk77mjqXlkI7vvIzliIbnsbvpk77mjqXkuK3kvJrlh7rnjrDkuIDkuKogL2NhdGVnb3J5LyDnm67lvZXjgILlpoLmnpzmg7PopoHljrvmjonliIbnsbvpk77mjqXkuK3nmoQgL2NhdGVnb3J5LyDnm67lvZXvvIzlj6~ku6Xkvb~nlKjmj5Lku7bjgILnvZHkuIrkuZ~mnInorrjlpJrmj5Lku7blj6~ku6Xnm7TmjqXljrvpmaTvvIjlhbblrp7lvojlpJrkuLvpopjlt7Lnu4~lhoXnva7kuobov5nkuKrlip~og73vvInjgILkvYbmmK~vvIzku4rlpKnluKbmnaXnmoTmmK~nuq8%2CsiteName_~5LiA57q45b-Y5b-n%2CsiteHost_www.ikxin.com%2Cp_Ii9hcnRpY2xlL3dvcmRwcmVzcy1yZW1vdmUtY2F0ZWdvcnki%2Cs_UgtJVcf4-xZbnnW8.png"
  "og:image:alt": "WordPress 纯代码去掉 URL 的 Category 分类前缀 - 一纸忘忧"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "WordPress 纯代码去掉 URL 的 Category 分类前缀"
  "twitter:image": "https://www.ikxin.com/_og/d/h_630%2Ca_~V29yZFByZXNzIOe6r-S7o-eggeWOu-aOiSBVUkwg55qEIENhdGVnb3J5IOWIhuexu-WJjee8gCAtIOS4gOe6uOW~mOW~pw%2Cc_Article%2Ctitle_~V29yZFByZXNzIOe6r-S7o-eggeWOu-aOiSBVUkwg55qEIENhdGVnb3J5IOWIhuexu-WJjee8gA%2Cdescription_~5oqY6IW-IFdvcmRQcmVzcyDnmoTlsI~kvJnkvLTku6zpg73nn6XpgZPvvIzkuIDkupvmsqHmnInpm4bmiJDljrvpmaQgL2NhdGVnb3J5LyDlip~og73nmoTkuLvpopjvvIzlnKjmr4~mrKHkv67mlLnlm7rlrprpk77mjqXlkI7vvIzliIbnsbvpk77mjqXkuK3kvJrlh7rnjrDkuIDkuKogL2NhdGVnb3J5LyDnm67lvZXjgILlpoLmnpzmg7PopoHljrvmjonliIbnsbvpk77mjqXkuK3nmoQgL2NhdGVnb3J5LyDnm67lvZXvvIzlj6~ku6Xkvb~nlKjmj5Lku7bjgILnvZHkuIrkuZ~mnInorrjlpJrmj5Lku7blj6~ku6Xnm7TmjqXljrvpmaTvvIjlhbblrp7lvojlpJrkuLvpopjlt7Lnu4~lhoXnva7kuobov5nkuKrlip~og73vvInjgILkvYbmmK~vvIzku4rlpKnluKbmnaXnmoTmmK~nuq8%2CsiteName_~5LiA57q45b-Y5b-n%2CsiteHost_www.ikxin.com%2Cp_Ii9hcnRpY2xlL3dvcmRwcmVzcy1yZW1vdmUtY2F0ZWdvcnki%2Cs_UgtJVcf4-xZbnnW8.png"
  "twitter:image:alt": "WordPress 纯代码去掉 URL 的 Category 分类前缀 - 一纸忘忧"
---

> Index of every page on this site: [llms.txt](https://www.ikxin.com/llms.txt)

# **WordPress 纯代码去掉 URL 的 Category 分类前缀**

2017.07.15 3,494  0

折腾 WordPress 的小伙伴们都知道，一些没有集成去除 `/category/` 功能的主题，在每次修改固定链接后，分类链接中会出现一个 `/category/` 目录。如果想要去掉分类链接中的 `/category/` 目录，可以使用插件。网上也有许多插件可以直接去除（其实很多主题已经内置了这个功能）。但是，今天带来的是纯代码版去掉 `/category/`，适合那些不喜欢用插件的小伙伴喔。

## PHP 代码

 PHP

```
// 去除分类 category 的代码
add_action('load-themes.php', 'no_category_base_refresh_rules');
add_action('created_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
function no_category_base_refresh_rules() {
    global $wp_rewrite;
    $wp_rewrite->flush_rules();
}
add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
    global $wp_rewrite, $wp_version;
    if (version_compare($wp_version, '3.4', '<')) {
        $wp_rewrite->extra_permastructs['category'][0] = '%category%';
    } else {
        $wp_rewrite->extra_permastructs['category']['struct'] = '%category%';
    }
}
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
    $category_rewrite = array();
    $categories = get_categories(array('hide_empty' => false));
    foreach ($categories as $category) {
        $category_nicename = $category->slug;
        if ($category->parent == $category->cat_ID)
            $category->parent = 0;
        elseif ($category->parent != 0)
            $category_nicename = get_category_parents($category->parent, false, '/', true) . $category_nicename;
        $category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
        $category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
        $category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
    }
    global $wp_rewrite;
    $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
    $old_category_base = trim($old_category_base, '/');
    $category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
    return $category_rewrite;
}
add_filter('query_vars', 'no_category_base_query_vars');
function no_category_base_query_vars($public_query_vars) {
    $public_query_vars[] = 'category_redirect';
    return $public_query_vars;
}
add_filter('request', 'no_category_base_request');
function no_category_base_request($query_vars) {
    if (isset($query_vars['category_redirect'])) {
        $catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
        status_header(301);
        header("Location: $catlink");
        exit();
    }
    return $query_vars;
}
```

将以上代码添加到你当前主题的 `functions.php` 文件中，紧接在最后一个 `?>` 前即可。

无论是安装插件还是使用代码，都可能会出现 404 页面错误，即 `%post_id%.html`（本站设置的固定链接）的伪静态失效了。这种情况下，只需重新保存一次固定链接设置即可。

## **评论**

(0)

暂无评论，来说两句吧

## **发布评论**

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://www.ikxin.com/#website","@type":"WebSite","description":"一纸忘忧的个人网站，关于技术探索与日常生活，记录 Web 开发、服务端与 DevOps 实践，分享软件工具、数码体验，以及代码之外的见闻与思考。","inLanguage":"zh-CN","name":"一纸忘忧","url":"https://www.ikxin.com/","workTranslation":[{"@id":"https://www.ikxin.com/zh-tw#website"},{"@id":"https://www.ikxin.com/ko#website"},{"@id":"https://www.ikxin.com/en#website"},{"@id":"https://www.ikxin.com/ja#website"}]},{"@id":"https://www.ikxin.com/article/wordpress-remove-category#webpage","@type":"WebPage","description":"折腾 WordPress 的小伙伴们都知道，一些没有集成去除 /category/ 功能的主题，在每次修改固定链接后，分类链接中会出现一个 /category/ 目录。如果想要去掉分类链接中的 /category/ 目录，可以使用插件。网上也有许多插件可以直接去除（其实很多主题已经内置了这个功能）。但是，今天带来的是纯","name":"WordPress 纯代码去掉 URL 的 Category 分类前缀","url":"https://www.ikxin.com/article/wordpress-remove-category","isPartOf":{"@id":"https://www.ikxin.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://www.ikxin.com/article/wordpress-remove-category"]}]}]}
```

## Sitemap

See the full [sitemap](https://www.ikxin.com/sitemap.md) for all pages.
