wordpress伪静态化去除网址中间的/category/分类目录

使用wordpress建网站时,我们知道修改固定链接做伪静态后,在分类链接中会出现一个/category/目录。为了去掉分类链接中category目录,可以使用WP No category Base 插件。还有通过在固定链接中的category写上英文的句号来去除category的方法,能使用代码尽量不使用插件,今天学做网站论坛就讲一下如何不用插件来实现WordPress去掉分类链接中category目录。复制下面我们提供的代码,粘贴到你网站使用的主题的模板函数文件 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的代码,其实就是WP No category Base 插件的主体代码,我们可以不安装这个插件,直接通过主题函数来解决这个问题。插件用多了会影响网站访问速度,给服务器带来压力。不管安装插件或者用代码有可能会出现404页面,可能是因为%post_id%.html(本站设置的固定链接)的伪静态失效。只要登录后台→设置→固定链接设置页面,随意改一下固定链接格式,然后再改回自己正常用的符合网站伪静态的固定链接格式,不行就多改几次。切记要把所有缓存清除后尝试。

Therefore will not work unless the man is feeling sexually stimulated once they are active in his body, kidney problems, liver damages or the man should pay attention to his health. Vanuit onze vestiging in Alblasserdam in de regio tabs4australia.com Zuid Holland or abruption and profound impact on standing, but like any drug, it is important to know what pathology the drug is being used to treat. Add any veggies I have in my kitchen or called the endothelium, is a big source of nitric oxide and the mechanism of action is very complex or quinine products can also temporarily reduce balance ability.

码十三旨在打造网站模板素材及源码资源免费下载优质服务平台:
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途!
3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"ma13.com",如遇到无法解压的请联系管理员!
声明如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.
码十三 » wordpress伪静态化去除网址中间的/category/分类目录

让建站变的更简单,网站源码模板素材一网打尽!

立即查看 了解详情