WordPress免插件去除分类category

后台→设置→固定链接,先更改为其他的,再更改回来/%post_id%.html,清空下浏览器缓存。

使用方法:将上述代码放到主题 functions.php 就可以了。

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

相关推荐

按月份调用阅读数最多的文章

第一步:为wordpress主题的文章添加记录浏览量功能 如何记录wordpress文章的用户浏览量,这个在我们发表的前面的文章中就已经介绍过了。这里,再把它贴出来: //访问计数:必须 启用 wp_head(……