WordPress不用插件怎么去掉分类链接中category

当使用wordpress构建网站时,我们知道在修改固定链接使其变为伪静态后,/category/目录也会出现在分类链接中。为了删除分类链接中的category目录,网上出现了各种用于删除category目录的wordpress插件,例如WP No category Base插件。 其中还有通过在固定

当使用wordpress构建网站时,我们知道在修改固定链接使其变为伪静态后,/category/目录也会出现在分类链接中。

为了删除分类链接中的category目录,网上出现了各种用于删除category目录的wordpress插件,例如WP No category Base插件。 其中还有通过在固定链接中的category写上英文的句号来去除category的方法。 这种方法访问链接没什么问题,但是前端代码显示的url却会多出一个点,因此不建议使用。

WordPress不用插件怎么去掉分类链接中category

对于不喜欢使用wordpress插件的站长来说,能使用代码尽量不使用插件,今天小编就讲一下如何不用插件来实现WordPress去掉分类链接中category目录。

去掉分类链接中category方法

复制下面的代码,粘贴到你网站使用的主题的模板函数文件 functions.php 文件中即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//去除分类标志代码
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();
}
// register_deactivation_hook(__FILE__, ‘no_category_base_deactivate’);
// function no_category_base_deactivate() {
//  remove_filter(‘category_rewrite_rules’, ‘no_category_base_rewrite_rules’);
//  // We don’t want to insert our custom rules again
//  no_category_base_refresh_rules();
// }
// Remove category base
add_action(‘init’, ‘no_category_base_permastruct’);
function no_category_base_permastruct() {
    global $wp_rewrite, $wp_version;
    if (version_compare($wp_version, ‘3.4’, ‘<‘)) {
        // For pre-3.4 support
        $wp_rewrite -> extra_permastructs[‘category’][0] = ‘%category%’;
    } else {
        $wp_rewrite -> extra_permastructs[‘category’][‘struct’] = ‘%category%’;
    }
}
// Add our custom category rewrite rules
add_filter(‘category_rewrite_rules’, ‘no_category_base_rewrite_rules’);
function no_category_base_rewrite_rules($category_rewrite) {
    //var_dump($category_rewrite); // For Debugging
    $category_rewrite = array();
    $categories = get_categories(array(‘hide_empty’ => false));
    foreach ($categories as $category) {
        $category_nicename = $category -> slug;
        if ($category -> parent == $category -> cat_ID)// recursive recursion
            $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]’;
    }
    // Redirect support from Old Category Base
    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]’;
    //var_dump($category_rewrite); // For Debugging
    return $category_rewrite;
}
// Add ‘category_redirect’ query variable
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;
}
// Redirect if ‘category_redirect’ is set
add_filter(‘request’, ‘no_category_base_request’);
function no_category_base_request($query_vars) {
    //print_r($query_vars); // For Debugging
    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;
}

原理介绍:

这段去掉分类链接中category的代码,就是WP No category Base 插件的主体代码,我们可以不安装这个插件,直接通过主题函数来解决这个问题。

注意事项:

不管安装插件或者用代码可能会出现404页面,即%post_id%.html(本站设置的固定链接)的伪静态失效了!

解决方法:

登录后台→设置→固定链接设置页面,随意改一下固定链接格式,然后再改回自己正常用的符合网站伪静态规则的固定链接格式,可以解决这个bug,不行就反复多改几次。

PS:如果还会出现404,建议把所有缓存清除后再尝试!ok,一切正常了。

您已阅读完《wordpress(共168篇)》专题的第 92 篇。请继续阅读该专题下面的文章:

    购买必读
    本站模板没有任何后门,也不会和我们服务器有任何通信,请您放心使用。
    本源码一次性买断,没有二次费用,并享受免费更新。
    在使用本站模板时,请不要使用我们的产品从事违反中华人民共和国法律、违反公序良俗的活动,否则我们有权拒绝服务,并配合有关单位予以处置。如果您不符合条件,请勿购买。
    售后是指通过QQ与客服联系处理问题,售后过期不影响您对主题的使用和更新。免费三个月售后服务。售后服务时间为工作日早9点到晚5点。如果不在线可以QQ留言,我们会尽快给您答复。
    售后服务包括程序的使用、BUG的处理、意见建议等。不包括技术服务、网站优化、二次开发、环境配置、服务器运维等。
    代码类商品属于数字化商品,具有可复制性,不支持退款,详见《网络交易管理办法》第二章 第16条 第三类。
    请仔细阅读我们的用户协议:用户协议

    给TA打赏
    共{{data.count}}人
    人已打赏
    wordpress教程

    WordPress怎么调用文章的第一张图片

    2023-12-9 15:46:29

    wordpress教程

    WordPress菜单导航栏如何自定义的

    2023-12-9 15:46:30

    0 条回复 A文章作者 M管理员
      暂无讨论,说说你的看法吧

    线

    专属客服

    (工作日 10:00 - 22:30 为您服务)

    2026-09-14 06:41:05

    您好,无论是售前、售后、意见建议……均可通过联系工单与我们取得联系。

    猜你想问:

    • 购买的模板免费包安装吗?

    • 这个演示地址有吗?

    • 购买vip会员可以下载哪些模板?

    您的工单我们已经收到,我们将会尽快跟您联系!
    取消

    您想了解哪方面的问题?

    选择聊天工具: