如何去掉WordPress分类链接中category的几种方法

很多使用WordPress博客程序搭建网站的站长应该都知道WordPress的分类链接里面有个category,从优化的角度考虑,多了一层category确实不友好,不利于SEO优化。对于网站的 seo 优化要求网站路径越浅越好,理论上说搜索引擎认为http(s)//:你的网址/category/76.html 的权重不如http(s)//:你的网址/76.html 的权重高,蜘蛛来访问的时候抓取后者速度要更快、更多页面,所以要去除 category,网站优化就是这些细节一点点积累起来,由量变到质变的。下面继续说去除 category 的方法。

WordPress如何去掉分类目录链接中的category?

最直接的方法是在设置——固定链接——分类目录前缀填写一个英文的“.”保存更改即可,如图所示:

可是这样设置有一个缺点,查看网页源代码,我们发现在链接中多了一个点“.”,其实这个办法用了后实际网页链接是http://你的网址/./news这样的,只是浏览器过滤了/./而已,显然这样对搜索引擎并不友好,所以这个做法虽然简单但是并不推荐。

那么我们如何把这个“.”也去掉呢?

比较常用的 category 插件很多,最出名的 WP No Category Base 官方已经找不到了,还有一款现在用的蛮多的 no category base wpml 插件可以使用来去除 category。在后台WP插件中心>安装插件>搜索安装No Category Base (WPML),或者进入插件页面直接下载上传安装,插件地址:https://wordpress.org/plugins/no-category-base-wpml/,插件安装启用即可完美解决这个问题了,如果不行的话建议刷新伪静态规则,方法:进入设置>固定链接,不用修改配置,直接点击保存按钮即可。

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

这款插件的使用前提是你的 wordpress 博客已经有内容了,才要去除 category,这时候用插件刚好。当然了插件会消耗系统资源,虽然不多总是看着不舒服。

代码去除 category(百度找教程时看到的,没测试过)

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

//去除分类标志代码
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();
}
// 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();
// }
// 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;
}

也不需要明白,照着做就可以自动去除 category 目录了。代码也是要在 wordpress 博客建立初期就添加进来,如果已经有内容且被搜索引擎收录的话,会导致前面已被收录的页面无法打开、降权等不好的影响。

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

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

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

版权声明:
作者:蓝逸轩
链接:https://www.12xf.cn/76.html
来源:星锋网
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>