持续高温!长沙所有初中新生停止军训!
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

我们的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
- SmartHost→$6.95 月 KVM-8GB 40GB 2TB 洛全球[VPS测评]
- 樊云→美国 cn2 VPS+免费 50G 防御+不限流量 40 元 月 美国VPS[主机]
- 一般般-AlexHost→€11.8 年 1.5GB 内存 10GB S虚拟空间(主机)
- 活动 HostXen→新用户送 20 元 日本直连 7M 不限流量 VP日本VPS[主机]
- FlipperHost→$4.5 月 OpenVZ-2GB 50GB 5全球[VPS测评]
- 优惠 ¥69 月 3 核 CPU 2G 内存 50G 硬盘 2Mbps 香港VPS[主机]
- 预售 $99 月 E3-1230V2 32G 内存 480G SSD 6香港VPS[主机]
- hostflyte→$12 年少量补货 洛杉矶 CN2 GIA 线路 K全球[VPS测评]
- friendhosting→新增中文界面 美国等 8 机房-不限流量 V美国VPS[主机]
- 用XAMPP搭建本地PHP测试环境及安装WordPress全球[VPS测评]
- 恒创科技 新用户优惠 香港CN2 GIA线路 1G内存 50g硬盘 2M香港VPS[主机]
- ChicagoVPS 512M内存20g硬盘5T大流量月付仅2.5刀 O全球[VPS测评]
- 如何为WordPress网站站外链接添加自动nofollow功能全球[VPS测评]
- Stromonic-印度孟买vps 测评 月付$5.2 1Gbps 1T全球[VPS测评]
- HKServerSolution 2核心 2G内存 20G HDD 10全球[VPS测评]
- HKServerSolution → 2G 内存 20G HDD 2T单全球[VPS测评]
- WordPress去掉分类链接中category目录的两种方法全球[VPS测评]
- 云服务器和服务器各有什么优缺点?全球[VPS测评]
- 晚点独家丨拼多多9月上线跨境电商平台,首站美国美国VPS[主机]
- 谷歌浏览器网址如何显示http,www,Chrome谷歌浏览器恢复地址栏全球[VPS测评]
- 者也vps年终特惠:全场云服务器7折终身优惠,赠1G内存,充1000送3全球[VPS测评]
- CombCloud,香港沙田cn2vps终身七折起,高质量网络稳定低延迟香港VPS[主机]
- JGKVM(极光kvm),便宜香港VPS(5G防御秒解),极光互联vps香港VPS[主机]
- earidc怎么样?香港三网cn2vps带宽1M月付29元香港VPS[主机]
- 创e云:香港CN2VPS优惠专区,4核/4G/20G/2Mbps不限流量香港VPS[主机]
- 租用美国服务器有哪些安全方案美国VPS[主机]
- LOCVPS,全新香港大浦CN2 VPS 2核4G内存3M仅54/月香港VPS[主机]
- 实例 GIA0910629396 重启失败,原因:sudo: unabl全球[VPS测评]
- sugarhosts首推DECADE云服务器,香港云服务器,1核512M香港VPS[主机]
- 尊云:河南BGP云服务器2核4G仅68.6元/月,823.2元/年;香港香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-232001.html











