中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

無需插件實現(xiàn)WordPress去掉分類鏈接中category (親測有效)

2018-11-02    來源:學做網(wǎng)站論壇

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

使用wordpress建網(wǎng)站時,我們知道修改固定鏈接做偽靜態(tài)后(相關(guān)知識:網(wǎng)站怎么做偽靜態(tài)設(shè)置),在分類鏈接中會出現(xiàn)一個/category/目錄。

為了去掉分類鏈接中category目錄,網(wǎng)上出現(xiàn)了各種去除category目錄的wordpress插件,比如WP No category Base?插件。還有通過在固定鏈接中的category寫上英文的句號來去除category的方法:WordPress去掉鏈接中的category。這種方法總感覺怪怪的,不推薦大家使用。

無需插件實現(xiàn)WordPress去掉分類鏈接中category

對于不喜歡使用wordpress插件的站來說,能使用代碼盡量不使用插件,今天學做網(wǎng)站論壇就講一下如何不用插件來實現(xiàn)WordPress去掉分類鏈接中category目錄。

去掉分類鏈接中category方法

復(fù)制下面我們提供的代碼,粘貼到你網(wǎng)站使用的主題的模板函數(shù)文件 functions.php 文件中即可:模板函數(shù)文件 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?插件的主體代碼,我們可以不安裝這個插件,直接通過主題函數(shù)來解決這個問題。

注意事項:

不管安裝插件或者用代碼可能會出現(xiàn)404頁面,即%post_id%.html(本站設(shè)置的固定鏈接)的偽靜態(tài)失效了!

解決方法:登錄后臺→設(shè)置→固定鏈接設(shè)置頁面,隨意改一下固定鏈接格式,然后再改回自己正常用的符合網(wǎng)站偽靜態(tài)規(guī)則的固定鏈接格式,可以解決這個bug,不行就反復(fù)多改幾次。

PS:如果還會出現(xiàn)404,建議把所有緩存清除后再嘗試!ok,一切正常了。

標簽: 代碼 建網(wǎng)站

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:WordPress調(diào)用某段時間評論最多的文章

下一篇:WordPress后臺框架WPD主題設(shè)置面板使用方法