WordPress如何去掉category链接

WordPress默认分类目录链接会带有category , 为了链接扁平化,层级不超过三级,更有利于Seo优化,所以要删掉category层级。

主题文件functions.php中加上下面代码即可。

//删除链接category目录
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;
}

本文来自投稿,不代表重蔚自留地立场,如若转载,请注明出处https://www.cwhello.com/14349.html

如有侵犯您的合法权益请发邮件951076433@qq.com联系删除

(0)
重蔚重蔚管理团队
上一篇 2019年10月30日 16:06
下一篇 2019年10月30日 16:23

相关推荐

  • 小编分享定制网站开发有什么优势。

        如今公司网站开发已变成很简单的事情,有的公司会选用定制,有的公司会选用模板。无论是选择哪一种都期望网站有个好的排名,有好排名的状况下,就必须有高品质的网站内容,高品质内容更易于蜘蛛的抓取。从而获…

    2023年6月16日
    00
  • 如何为WordPress添加动态侧边栏。

    熟悉WordPress的人应该都知道,侧边栏是一个非常重要的区域,你可以用来它展示热点文章、添加联系方式、嵌入广告社交媒体等信息,更好地利用网站 不过WordPress默认的侧边栏有一点局限,它只能设置整个网站的侧边栏…

    2022年10月24日 wordpress开发
    025
  • 小编教你网站优化的文章,关键词是重点吗。

    网站优化的文章应该怎么写?这已经是老生常谈的问题了,还是那个问题,原创度和实用性!现在越来越多的人做网站公众号之类,我们除了要点击量,还希望我们的文章能够精准的击中顾客,更多的还是能带来优质的顾客流…

    2023年3月11日
    01
  • 小编教你网站的主题怎么明确。

    主题,是一个网站非常重要的一点,如果我们在没有主题的情况下进行优化,更新,那么很可能都会偏离原来的航道,所以我们要做好网站主题的明确,那么怎么做呢?今天小编为大家分享一下。 一、用户角度 中国有上亿的互…

    2023年6月9日
    00
  • WordPress屏蔽搜索引擎方法。

    在刚刚开始建站时,因为站点结构和初始内容都没有准备好,网站处于测试阶段,我们开放搜索引擎收录是没有意义的,如果搜索引擎收录了很多无用的页面,反而不利于我们后期的优化,所以在初建网站时,禁止搜索引擎是…

    2022年10月24日 wordpress开发
    038
  • 教你网站开发流程介绍。

        网站开发完全从零开始一步一步慢慢发展到线上,每一个步骤都是至关重要的,第一步的准备工作要做好,前端和后端也需要谨慎处理,下面简单介绍一下网站开发流程。    一、必不可少的准备工作    网站开发的目的…

    2023年6月16日
    02
  • 分享汕头网站开发交互设计是什么。

        对于互联网了解不多的汕头网站开发公司来说,也一定是听说过交互设计的,但是当深入了解网站的交互设计到底是什么的时候,大家可能完全说不上来,那么汕头网站开发的交互设计到底是什么呢?今天就让我们一起了解…

    2023年6月16日
    00
  • 只有预先对网站开发功能进行安全评估,才能避免网站被篡改。

    企业网站遭到攻击,主页文件被篡改,百度快照也被劫持并跳转到其他网站。企业网站首先要做的就是保护网站的安全。然而,许多企业并不了解如何建立网站的安全性。在安全方面,他们需要找到专业的网站安全公司来保护…

    2023年2月20日
    01

联系我们

QQ:951076433

在线咨询:点击这里给我发消息邮件:951076433@qq.com工作时间:周一至周五,9:30-18:30,节假日休息