默認情況下,WordPress分類的永久鏈接是這樣的比如本站的技術文章分類。
exehack.net/category/my-article/
如果能把中間那段Category去掉,是不是更美觀切更利于SEO優化呢:
exehack.net/my-my-article/
去除category的效果圖:
小遍發現了二種方法:
1.開始本站就是啟用的WP No Category Base插件,使用它地將Wordpress強制加入的分類鏈接格式去掉。
插件特性:
1.將一級目錄和二級目錄永久鏈接格式優化為
exehack.net/my-my-article/
exehack.net/software/black-soft
以下是官網給出的這款插件的介紹:
1. 使用非常簡單-幾乎不會添加任何額外負擔
2. 工作非常順暢-無需任何設置
3. 無需修改wordpress 文件
4. 不需要任何其他插件就能工作
5. 與sitemap插件兼容
6. 對多級分類同樣起作用。
PS:以上說法都沒錯,可是這苦逼的作者為什么不加上一條”該插件使用后無法停用或卸載”否則貨照成網站文章無法訪問”。
相信大家都有同感為什么明明是去除分類的category和文章頁的url有什么關系?
這款插件的確很不錯,可是小編非常討厭流氓的插件,可是確無法停用.
經過小編的苦苦尋找終于發現了然后解決停用WP-No-Category-Base插件后文章頁無法打開的方法。
方法非常簡單:
1.把【固定連接】改為【默認】狀態,然后,關閉【WP NO category base】插件
2.再改回之前的URL形式就可以了。
3.最后你就可卸載掉該插件了。
既然是要卸載掉【WP NO category base】插件的話小編早已找到新的方法來去除分類頁面的category。
否則文章全部無法訪問全是404錯誤頁面大家辛辛苦苦經營起來的網站豈不是全毀了。
2.所以這里就介紹另外一種方法通過在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;
- }