PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > CMS教程 > wordpress教程 > 正文

    wordpress去掉分类category标志代码实现版本

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:其实网上很多去掉分类标志的方法,比如固定链接前加一个小点.,亦或者是修改wordpress原程序文件的方法,感觉最好用的就是使用插件:no-cat...
    其实网上很多去掉分类标志的方法,比如固定链接前加一个小点“.”,亦或者是修改wordpress原程序文件的方法,感觉最好用的就是使用插件:no-category-base,使用改插件,不仅能将category标志去掉,而且访问之前带category的分类目录还有做301跳转,这非常利于SEO。不管怎么说,如果你能看到这篇文章,那么就是只想实现这个功能,并且不想用插件,引用奶嘴大大的一句话,我相信大家看完就能明白:

      其实在wordpress里,不管你有没有启用插件和主题,只要你把它们上传wordpress里了,wordpress就会在后他把那些插件和主题的信息读取出来再显示。那么这样的话wordpress每读取一个插件的信息就要历遍一次文件(PS:因为wordpress插件没有固定的信息存放文件,只要插件作者高兴,插件目录下的任意一个php文件都是可以的。所以wordpress只能通过历遍文件来读取信息。),而主题信息呢?都在style.css里,也就是说每个主题存在于服务器上的主题wordpress都会把他们的style.css读取一遍,那么数量多的话wordpress的速度也就慢下来了,所以小V建议主题和插件能不用的都不用并且删除掉。插件能集成到主题的尽量集成到主题,因为这样就可以减少wordpress历遍文件的次数了。

      如果你认真看完了上面一篇文章,你就能明白,为何会有这篇文章了,哈哈。废话不多说,我们的目的就是移植插件no-category-base到主题的functions.php中。

      如何移植插件?

      恩,这是个问题,如果你也是只是想得到结果的伸手党,好吧,跳转到文章最后面复制代码粘贴到你的functions.php中就可以了,接下来说说插件移植到functions.php中的注意事项,注意,因为本人也是wp折腾菜鸟,所有有错的地方,望大神指出来:

      其实最主要的就是这个register_activation_hook,这个是插件注册的时候激活,所以直接丢到functions.php里肯定会出错,丢到主题中,我们就将此处修改为主题激活的时候启用,那么相应的就为load-themes.php,好了重点完了。接下来直接看代码,我相信大家就会明白了。

    wordpress去掉分类category标志代码实现版本

    去除分类标准category代码版

      注释的地方就是插件禁用的时候移出伪静态规则,这里我们不需要他,因为主题未启用就相当于没有启用这个伪静态规则,语文不好,表述不清。你懂得。将下面的代码复制到你主题的functions.php中即可去掉分类标志:
     

     
    复制代码 代码如下:
    /* 
    *去除分类标志代码,来自wordpress主题
    */
    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;  
    }  

    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-103-1452-1.html
    相关热词搜索: