YYYHost:4核4G香港cn2vps,20G SSD/3Mbps不限流量/KVM,月付40元
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

我们的wp打开分类目录显示的链接默认是加上了/category/ 目录标志,非常的不美观,url非常的长,不利于url的SEO,网上多多方法,总结两种,一种是 WP No Category Base 插件,一种是手动编辑主题函数文件。
不管安装插件或者用代码可能会出现404页面,即%post_id%.html(本站设置的固定链接)的伪静态失效了!
解决方法:登录后台→设置→固定链接设置页面,随意改一下固定链接格式,然后再改回自己正常用的固定链接格式,可以解决这个bug,不行就反复多改几次。
方法一、WP No Category Base 插件
WP No Category Base 插件功能简单,就是仅仅为了去除 /category/ 目录标志,直接安装,不需要任何设置就可以使用。如果你的是新站,可以考虑去除/category/ 目录;如果你的是老站(也就是说搜索引擎已经收录了分类目录),使用这个插件后,注意做一下分类链接的301跳转。
方法二、非插件
不用插件来实现WordPress去掉分类链接中category目录,只需要将下面代码添加到你当前主题的 functions.php 文件中即可:
打开文件,移动到最尾部粘贴
//去除分类标志代码
add_action( ‘load-themes.php’, ‘no_category_base_resh_rules’);
add_action(‘created_category’, ‘no_category_base_resh_rules’);
add_action(‘edited_category’, ‘no_category_base_resh_rules’);
add_action(‘delete_category’, ‘no_category_base_resh_rules’);
function no_category_base_resh_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_resh_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;
}
不管安装插件或者用代码可能会出现404页面,即%post_id%.html(本站设置的固定链接)的伪静态失效了!
解决方法:登录后台→设置→固定链接设置页面,随意改一下固定链接格式,然后再改回自己正常用的固定链接格式,可以解决这个bug,不行就反复多改几次。
[主机测评]历史优惠活动内容
猜你可能想看的VPS
- hostodo→$40 年 送 DirectAdmin KVM 2G 内全球[VPS测评]
- onevps 推出日本专业奈飞(Netflix)VPS 首月半价 10T日本VPS[主机]
- 在 Ubuntu 系统下安装最新稳定版 qBittorrent全球[VPS测评]
- CentOS 7 环境下安装 MySQL全球[VPS测评]
- 促销 n3servers→E5-2620v2 服务器仅售$19.5 月 独立服务器[U]
- SoftShellWeb→$12.99 年 256MB 内存 10GB 虚拟空间(主机)
- thinkphp IP 获取和定位全球[VPS测评]
- CN2 $15 年 512M 内存 10G 硬盘 1T 流量@1Gbps全球[VPS测评]
- Excel 快速计算排名的两种方式→相同成绩占用名次及相同成绩不占用名次全球[VPS测评]
- 免费 VPS goorm→免费韩国 美国 NAT VPS 速度一般 10韩国VPS[主机]
- Vultr VPS主机取消 $3.5入门机型及新用户赠送$25全球[VPS测评]
- HostYun 全场9折优惠 月付15.3元 大带宽 512M 10G 全球[VPS测评]
- 用XAMPP搭建本地PHP测试环境及安装WordPress全球[VPS测评]
- Hostodo美国独立日促销,NVME VPS七折优惠,年付$13起,赠独立服务器[U]
- 微基主机520常规优惠 虚拟主机2G三年付200元全球[VPS测评]
- 2022年最新的15个美容院抖音文案,美容院文案怎么写全球[VPS测评]
- 腾讯会议故障了 提示:会议发生异常请重新加入会议全球[VPS测评]
- 向日葵-漏洞科普:海外云服务器三种漏洞修复方法快收藏起来!全球[VPS测评]
- 谷歌浏览器网址如何显示http,www,Chrome谷歌浏览器恢复地址栏全球[VPS测评]
- 适合美容院的正能量抖音文案,唯美阳光,正能量满满全球[VPS测评]
- 吸血鬼崛起服务器怎么快速搭建?全球[VPS测评]
- 搬瓦工:美国香港cn2 gia线路VPS,10Gbps带宽,移动+联通企美国VPS[主机]
- 2021硅云新年献礼活动:香港企业级云服务器2核4G5M仅2888元/5香港VPS[主机]
- SugarHosts,美国云服务器,洛杉矶cn2中美极速专线,香港建站服美国VPS[主机]
- 速云互联:香港/美国CN2 GIA线路/BGP/10Mbps/全场八折,美国VPS[主机]
- 自适应bootstrap左侧导航可关闭全球[VPS测评]
- 云服务器能干什么?有几十种玩法?阿里云,腾讯云服务器如何购买?全球[VPS测评]
- edgeNAT:全场6折优惠,韩国CN2、美国CN2、香港CN2月付36韩国VPS[主机]
- 酷锐云双十一活动:美国vps、香港vps云主机,2核4G4M,50元/首美国VPS[主机]
- Fatal error: Allowed memory size of 全球[VPS测评]
转载请注明原文地址:http://140.238.13.167:12355/read-237109.html











