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

当使用wordpress构建网站时,我们知道在修改固定链接使其变为伪静态后,/category/目录也会出现在分类链接中。
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,一切正常了。

温馨提示:本文最后更新于 2023-12-09 15:46 ,某些文章具有时效性,若有错误或已失效,请在下方留言或联系QQ115904045
声明:
1.本站大部分内容均收集于网络!若内容若侵犯到您的权益,请发送邮件至:[email protected],我们将第一时间处理!
2.资源所需价格并非资源售卖价格,是收集、整理、编辑详情以及本站运营的适当补贴,并且本站不提供任何免费技术支持
3.所有资源仅限于参考和学习,版权归原作者所有,更多请阅读网站声明

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

WordPress调用指定分类热门文章的两种方法

2023-12-9 15:46:28

wordpress教程

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

2023-12-9 15:46:30

  • 0 条回复 A文章作者 M管理员
      暂无讨论,说说你的看法吧
    购物车
    优惠劵
    今日签到
    有新私信 私信列表
    搜索

    夕阳无别事,等风也等你

    联系我们