WordPress 博客去除 category 的几种方法

WordPress 默认路径自带 category 目录,如果不去除最后文章网址会多一个/category/,为了去掉这个无用的路径,我们可以采取插件和代码的方法。本来本文内容是网络上到处都有的,插件和代码多到烂大街的地步了,但是偏偏晚上就有群友问到wordpress 博客如何去除 category,老魏去魏艾斯博客搜索了一番也没找到,看来是应该在博客里面记录一下,留着以后用了。

对于网站的 seo 优化要求网站路径越浅越好,理论上说搜索引擎认为/category/7237.html 的权重不如/7237.html 的权重高,蜘蛛来访问的时候抓取后者速度要更快、更多页面,所以要去除 category。实际上这些年搜索引擎也在不断发展进化,只要你的网页路径不是四五层那种深度,一般抓取都没问题,但是咱能提前给优化的还是做了呗,网站优化就是这些细节一点点积累起来,由量变到质变的。说说就跑题了,下面继续说去除 category 的方法。

一、最常见的是在 wordpress 后台>>设置>>固定链接>>可选>>分类目录前缀的空格里,输入英文状态的.再保存,这样可以轻松去除 category。弊端是如果你网站有内容之后做,因为网页路径发生变化了,导致原来带 category 的网页打不开,搜索引擎蜘蛛抓取也会失败,可能会对网站降权。

在 WordPress后台>设置>固定链接中,先在自定义结构的路径中添加  /%category% 标签,后面跟着你自定义的结构,如下图所示:

在 WordPress后台>设置>固定链接中添加  /%category% 标签” class=”wp-image-28464″></figure>
<p>然后在下面的分类目录前缀添加英文 . 符号即可。</p>
<figure class=在分类目录前缀添加英文 . 符号

用了后实际网页链接是 http://www.vpsss.net/7237.html 这样的,这个做法简单好用。

二、插件

比较常用的 category 插件很多,最出名的 WP No Category Base 官方已经找不到了,还有一款现在用的蛮多的 no category base wpml 插件可以使用来去除 category。

插件功能简单,就是为了去除 /category/ 目录,安装后不需要任何设置就可以使用。该插件还把旧的分类链接自动 301 重定向到新链接地址。

这款插件的使用前提是你的 wordpress 博客已经有内容了,事后诸葛亮才要去除 category,这时候用插件是比较合适的。

当然了插件会消耗系统资源,虽然不多总是看着不舒服。

三、代码去除 category

代码放到 wordpress 模板的 functions.php 文件中,内容如下:

// 去除 category 代码
if( _hui(‘no_categoty’) && !function_exists(‘no_category_base_refresh_rules’) ){
register_activation_hook(__FILE__, ‘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();
}
// 移除 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;
}
// For Debugging
//add_filter(‘rewrite_rules_array’, ‘no_category_base_rewrite_rules_array’);
//function no_category_base_rewrite_rules_array($category_rewrite) {
// var_dump($category_rewrite); // For Debugging
//}
// 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 目录了。代码也是要在 wordpress 博客建立初期就添加进来,如果已经有内容且被搜索引擎收录的话,会导致前面已被收录的页面无法打开、降权等不好的影响。

这段代码内容其实就是 WP No category Base 插件的主要代码,所以不安装这个插件,扔到主题函数里一样能解决这个问题。

提醒: 使用代码之后,网站可能会出现 404 页面,也即%post_id%.html(本站的固定链接)的伪静态失效了,解决办法很简单,登录后台>>设置>>固定链接设置页面,把固定链接格式改成别的,然后再改回自己常用的格式,保存一下就可以解决这个 bug,不行就多改几次。如果还不行就把所有缓存清除后再尝试。

另外还有修改 category 文件的方法不建议使用,因为每次系统升级都会覆盖掉文件,还要重新修改不是一劳永逸的方法。总之如果博客内容被收录很多了(老站)就用插件,然后注意一下分类目录要 301 重定向;建站初期(新站)没收录就用代码一次性解决。

温馨提示: 本文最后更新于2024-06-16 19:03:32,某些文章具有时效性,若有错误或已失效,请在下方 留言或联系 Ferry资源网
© 版权声明
THE END
喜欢就支持一下吧
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容