当前所在位置: 首页>>百科全书

无需插件实现WordPress去掉分类链接中category (亲测有效)

发布时间:2025-10-19 15:18:15 本站作者 【 字体: 】 浏览:6 次

使用wordpress建网站时,我们知道修改固定链接做伪静态后,在分类链接中会出现一个/category/目录。

为了去掉分类链接中category目录,网上出现了各种去除category目录的wordpress插件,比如WP No category Base 插件。还有通过在固定链接中的category写上英文的句号来去除category的方法。这种方法总感觉怪怪的,不推荐大家使用。

无需插件实现WordPress去掉分类链接中category

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

去掉分类链接中category方法

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

模板函数文件 functions.php

//去除分类标志代码
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,一切正常了。


上一篇:解决 PHP 7.2 环境中 WordPress 的 count() 调用问题

下一篇:WordPress网站临时关闭站点503

广告位-300PX*250PX
最新发布
office2010密钥,office2010激活密钥,office2010永久密钥激活码最新分享

office2010密钥,office2010激活密钥,office2010永久密钥激活码最新分享

office2010密钥,office2010激活密钥,office2010永久密钥激活码最新分享
不少用户在求2018最新Microsoft Office 2010产品密钥,office2010密钥有两种,office2010永久密钥和免费版office密钥,office2010激活码最新的已发布很快就失效了,只有kms客户端免费版office激活密钥是永久有效的。下面小编为大家整理分享office2010最新密钥序列号,不区分32位和64位。说明:office2010零售版密钥和kms密钥...
office365激活密钥,小编分享2025最新office365激活码

office365激活密钥,小编分享2025最新office365激活码

office365激活密钥,小编分享2025最新office365激活码
office365是微软办公套件office的年度服务,那么最近有用户想要office365激活码,过通整理,下面小编把2018年最新office365激活码和激活电脑上office365软件的方法分享给大家。2018年 office365激活码大全:RCBQN-J4MK8-VV3XJ-BPPYB-TXVT77TPNM-PMWKF-WVHKV-G869H-9BQ6XDMXHM-GNMM3-MYHHK-6TVT2-XTKKKG9N...
office2018产品密钥,小编告诉你office2018怎么激活

office2018产品密钥,小编告诉你office2018怎么激活

office2018产品密钥,小编告诉你office2018怎么激活
office2018是微软公司即将发布的一款全新的办公自动化软件,可以说各个行业都必备的;office2018全新版本功能全面升级,具有强大的数据处理、数据计算、图形编辑、演示文稿的制作和文档排版等功能,那么哪里有office2018产品密钥?下面,小编给大家带来了office2018产品密钥。office2018是微软公司开发的一款基于Windows操作...
怎么激活office2019最新版?office 2019激活秘钥+激活工具推荐

怎么激活office2019最新版?office 2019激活秘钥+激活工具推荐

怎么激活office2019最新版?office 2019激活秘钥+激活工具推荐
最新激活密钥GRBR4-J4N3M-KTX9C-JM4PJ-J8HPT (小编使用的是此激活密钥)NJHVR-FK6XR-M97MB-P8CXD-FM4PJNXVJY-X9T8G-Y6RWG-8VQ7V-PWHPTW8W6K-3N7KK-PXB9H-8TD8W-BWTH9N9J9Q-Q7MMP-XDDM6-63KKP-76FPM9DP6T-9AGWG-KWV33-9MPC8-JDCVF7G2HE-JR8KL-ABB9D-Y7789-GLNFLU2PWU-H7D9H-69T3B-JEYC2-3R2NGR8R8P-MTT6F-KLRPM-J7CAB-PJM8CA4...