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

相关推荐

  • WordPress常用的函数、方法汇总

    选择使用 WordPress来搭建博客,主要原因便在于 WordPress有较高的流行度,还有各种围绕其进行的开发的扩展功能应有尽有,基本上可以说我们在博客建设中所需的任何功能,都已有人想到并得到实现,让你不必在博客具…

    2019年5月14日
    0355
  • 我来说说7款优秀的WordPress优化加速插件。

    以下是一些优秀的WordPress优化加速插件:WP Super Cache和Cache Enabler,这两款都是缓存插件,可以为网站生成缓存,以此提升网站速度。另外还有如WP Rocket、W3 Total Cache等也是优秀的选择。这些插件可以帮助您…

    2024年7月18日
    00
  • 最适合初学者的WordPress SEO实践

    初学者到SEO面临很多挑战。在开始时有很多事情要做和学习,这使得无法区分重要和应该首先完成的任务以及可以在以后实施的最不重要的任务。 确实,网上有很多关于\'WordPress SEO\'的信息。 问题在于,如果您不熟悉W…

    2019年8月8日 SEO操作
    0337
  • 检查wordpress文章页面显示是否已被百度收录的方法

    将以面代码添加到当前主题的 functions.php 下即可: /* 判断当前文章是否被百度收录,若没有被收录则可点击提交至百度,加速收录!(此插件在文章页面仅管理员可见) */ function d4v($url){ $url='http://www.baidu…

    2017年11月30日
    0254
  • WordPress自动获取内容中的图片作为缩略图调出

    我们有一些WordPress主题和排版的要求,是需要将缩略图展现的,这里我们可以有手动设置缩略图的。但是有些时候我们采编的内容内容中有图,但是没有设置缩略图。这里我们可以直接用自动调用文章中的图片作为缩略图。…

    2025年1月8日
    08
  • WordPress关闭Pingback和Trackpack通知方法。

    当我们的WordPress博客文章被他人引用时,默认情况下会产生一条Pingback通知,并且会在评论区留下相关链接,但是有时候我们不想接收这样的通知,这时可以把WordPress的Pingback功能关闭,在本文中我们来教大家如何…

    2022年10月24日 wordpress开发
    021
  • WordPress 置顶文章的3种方法。

    在使用WordPress搭建博客时,默认情况下会按时间对文章进行排序,每次发布新的文章都会自动展示在博客的第一个位置,这样网站有新文章了就可以让访客第一时间看到 但是有些时候我们并不想在网站的顶部展示最新的文…

    2022年10月24日 wordpress开发
    020
  • 分享大同网站开发时,如何选择背景图。

        在大同网站开发时以前先提前准备好2个工作中,对设计师们会出现协助:    1、 大同网站开发方案中常需图片种类    在图片设计方案前,网站开发设计师能参照现阶段现有的网页粗略地合理布局和频道等,再次整理列…

    2023年6月16日
    08

联系我们

QQ:951076433

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