243
文章
15
说说
218
评论
248464
访问

个人博客网站:prettywordpress.com(吃饭用大碗的程序猿)

最新评论
qq_avatar
1年前,”四五设计网”在《如何使用百度云CDN优化wordpress速度》
说:来学习一下,应该用得上
qq_avatar
2年前,” ”在《友情链接》
说:像不能用了更新地址为 https://aba.pet/wp-content/uploads/2022/05/favicon-1.gif
qq_avatar
2年前,”Alex”在《终于找到拖慢网站的罪魁祸首》
说:优化这么快,你有想过我们的感觉?
qq_avatar
3年前,”ヘル”在《码农=新生代农民工》
说:打卡@[huaixiao]
qq_avatar
3年前,”肋巴骨”在《Win10系统桌面一直闪屏自动刷新怎么办》
说:不错啊,这是谁写的教程,真棒
A+

wordpress无插件去掉分类链接中category方法

标签: 最后编辑:2020年5月6日

在主题的function里面添加如下代码,因为我的博客加了看板娘和数字滚动效果,链接中多了category就会找不到路径,所以只有去掉category

下面代码来自WP No category Base 插件的主体代码,添加到主题function里面就可以不用安装插件啦啦啦啦

//去除分类标志代码
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', '<')) {
        // 用于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) {
    //var_dump($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]';
    //var_dump($category_rewrite); // 用于调试
    return $category_rewrite;
}
// 添加“category_redirect”查询变量
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) {
    //print_r($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;
} 

注意事项:

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

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

PS:如果还会出现404,建议把所有缓存清除后再尝试!

PS:什么是伪静态呢?简单的解释就是将网站所有动态网页链接地址都转化为静态网页,方便搜索引擎蜘蛛爬行和抓取。

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

发表一下你的评论呗

回复评论代表你同意网站的 隐私政策

... 友情提示 请保留版权标识
复制成功!
目录