wordpress去掉分类category

wordpress去掉分类category
No Category Base 这款插件就可以实现去掉Category的功能,是一款很不错的插件
插件还是能不用就不用,插件用的多了,程序运行速度也会有影响的
代码是从 No Category Base 中直接复制出来的,放到模板文件functions.php中既可。

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

*邮箱和昵称必须填写

    空空如也!

消息盒子
# 您需要首次评论以获取消息 #
# 您需要首次评论以获取消息 #

只显示最新10条未读和已读信息