六六云:1核1GB/15GB空间/1T流量/100Mbps/KVM/香港PCCW,50元/月
![]() | ![]() | ![]() | ![]() |
| 【性价之王】 | 【线路之王】 | 【价格之王】 | 【配置之王】 |
| 【免费之王】 | 【香港首推】 | 【梯子之王】 | 【独服之王】 |

我们的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
- 商家投稿 ZJI→美西服务器 香港服务器 日本服务器限量 7 折优惠日本VPS[主机]
- 真实测评 tmhhost 洛杉矶 CERA 机房测评 tmhhost 高全球[VPS测评]
- 储存 VPS $8.5 月 1G 内存 1T 硬盘 100Mbps 不限全球[VPS测评]
- 一个粗的环字一个窄的燕字是什么成语?全球[VPS测评]
- Krypt 旗下新品牌 iON→开业促销 全场 VPS 年付 5 折优惠全球[VPS测评]
- 景文互联→云服务器全场 8 折 年付内存翻倍 充值 1000 元送 30全球[VPS测评]
- js 动态改变文本颜色全球[VPS测评]
- 腾讯云双十一提前购→1C2G1M 年付 88 元 2C4G5M 三年仅 全球[VPS测评]
- [黑五]CloudCone→$15 年 KVM-512MB 20GB 2全球[VPS测评]
- 便宜 VPS $1.95 月 512M 内存 5G SSD 100Mbp全球[VPS测评]
- 香港直连 ¥432 年 2 内存 30G 硬盘 300G 流量 30Mb香港VPS[主机]
- 投稿 数脉科技四月促销→E3-1230v2 16G 240G SSD 1香港VPS[主机]
- PacificRack→$13.5 年 KVM-1GB 35GB 1TB全球[VPS测评]
- Error: php55w-common conflicts with 全球[VPS测评]
- 特价 桔子 VPS→洛杉矶 CN2 VPS 三网直连 回程 CN2 月付全球[VPS测评]
- WordPress删除文章时怎么才能删除缩略图及图片附件全球[VPS测评]
- 知更鸟Begin WordPress主题美化修改教程全球[VPS测评]
- CrownCloud → 512M内存 500G HDD 3T流量 1G全球[VPS测评]
- Wikihost(idc.wiki) → 香港HE线路促销 -非直连大香港VPS[主机]
- [谨慎]Tsukearu → 年付半价免初装费的日本小鸡 → 年付 ¥3日本VPS[主机]
- 又有美议员窜访台,中国驻美大使馆:“中方将对美国的挑衅采取果断的反制措施美国VPS[主机]
- 瓜云互联怎么样?香港/美国洛杉矶CN2高防vps 50G月付34元美国VPS[主机]
- tudcloud,香港沙田大带宽vps特惠7折,CN2+BGP直连,10香港VPS[主机]
- kvmcloud怎么样?香港BGP云服务器 月付12.9元香港VPS[主机]
- 二三互联年末回馈:香港/美国云服务器新购特价促销,优惠码可打76折美国VPS[主机]
- HostKVM香港云地VPS,2核4G内存折后$8.4/月,30M大带宽香港VPS[主机]
- 全球云lovps,香港CN2线路vps 新品七折月付49元香港VPS[主机]
- 二三互联年末回馈:香港/美国云服务器新购特价促销,优惠码可打76折美国VPS[主机]
- uuuvps:香港大带宽vps低至239元/年;香港服务器720元/e3香港VPS[主机]
- 七安云怎么样?香港CN2+BGP线路VPS 1核1G10M 16/月香港VPS[主机]
转载请注明原文地址:http://140.238.13.167:12355/read-241529.html











