No Category Base (WPML)去除分类category前缀插件

有SEO需求并且对URL样式有特殊最求的人都希望去掉WordPress分类链接中的 /category/ 目录标志,网上很多这方面的教程,最简单的方式就是使用 No Category Base (WPML) 插件(或类似插件),例如本站就是去掉了/category/ 目录的,以前我也是使用的 No Category Base (WPML) 插件。有时候为了偷懒还是会直接使用No Category Base (WPML)。

但是除了用插件还有一个代码的实现方式是我目前比较喜欢的,因为可以整合进入主题中,所以使用起来非常的方便,代码请放入主题的functions.php文件中

集成移除WordPress分类category代码实现方法:

  1.     add_action( 'load-themes.php',  'no_category_base_refresh_rules');
  2.     add_action('created_category', 'no_category_base_refresh_rules');
  3.     add_action('edited_category', 'no_category_base_refresh_rules');
  4.     add_action('delete_category', 'no_category_base_refresh_rules');
  5.     function no_category_base_refresh_rules() {
  6.         global $wp_rewrite;
  7.         $wp_rewrite -> flush_rules();
  8.     }
  9. // Remove category base
  10.     add_action('init', 'no_category_base_permastruct');
  11.     function no_category_base_permastruct() {
  12.         global $wp_rewrite, $wp_version;
  13.         if (version_compare($wp_version, '3.4', '<')) { // For pre-3.4 support $wp_rewrite -> extra_permastructs['category'][0] = '%category%';
  14.         } else {
  15.             $wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
  16.         }
  17.     }
  18. // Add our custom category rewrite rules
  19.     add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
  20.     function no_category_base_rewrite_rules($category_rewrite) {
  21.         //var_dump($category_rewrite); // For Debugging
  22.         $category_rewrite = array();
  23.         $categories = get_categories(array('hide_empty' => false));
  24.         foreach ($categories as $category) {
  25.             $category_nicename = $category -> slug;
  26.             if ($category -> parent == $category -> cat_ID)// recursive recursion
  27.                 $category -> parent = 0;
  28.             elseif ($category -> parent != 0)
  29.                 $category_nicename = get_category_parents($category -> parent, false, '/', true) . $category_nicename;
  30.             $category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
  31.             $category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
  32.             $category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
  33.         }
  34.         // Redirect support from Old Category Base
  35.         global $wp_rewrite;
  36.         $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
  37.         $old_category_base = trim($old_category_base, '/');
  38.         $category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
  39.         //var_dump($category_rewrite); // For Debugging
  40.         return $category_rewrite;
  41.     }
  42. // Add 'category_redirect' query variable
  43.     add_filter('query_vars', 'no_category_base_query_vars');
  44.     function no_category_base_query_vars($public_query_vars) {
  45.         $public_query_vars[] = 'category_redirect';
  46.         return $public_query_vars;
  47.     }
  48. // Redirect if 'category_redirect' is set
  49.     add_filter('request', 'no_category_base_request');
  50.     function no_category_base_request($query_vars) {
  51.         //print_r($query_vars); // For Debugging
  52.         if (isset($query_vars['category_redirect'])) {
  53.             $catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
  54.             status_header(301);
  55.             header("Location: $catlink");
  56.             exit();
  57.         }
  58.         return $query_vars;
  59.     }
喜欢