WordPress Catalog Removal Category

Source: Internet
Author: User
Demo Effect: http://houjinzhe.com/news/
  1. /*
  2. * Remove the classification Mark code
  3. */
  4. Add_action (' load-themes.php ', ' no_category_base_refresh_rules ');
  5. Add_action (' created_category ', ' no_category_base_refresh_rules ');
  6. Add_action (' edited_category ', ' no_category_base_refresh_rules ');
  7. Add_action (' delete_category ', ' no_category_base_refresh_rules ');
  8. function No_category_base_refresh_rules () {
  9. Global $WP _rewrite;
  10. $WP _rewrite-Flush_rules ();
  11. }
  12. Register_deactivation_hook (__file__, ' no_category_base_deactivate ');
  13. function No_category_base_deactivate () {
  14. Remove_filter (' category_rewrite_rules ', ' no_category_base_rewrite_rules ');
  15. We don ' t want to insert our custom rules again
  16. No_category_base_refresh_rules ();
  17. // }
  18. Remove Category Base
  19. Add_action (' init ', ' no_category_base_permastruct ');
  20. function No_category_base_permastruct () {
  21. Global $WP _rewrite, $WP _version;
  22. if (Version_compare ($wp _version, ' 3.4 ', ' < ')) {
  23. For pre-3.4 Support
  24. $WP _rewrite-extra_permastructs[' category '][0] = '%category% ';
  25. } else {
  26. $WP _rewrite-extra_permastructs[' category ' [' struct '] = '%category% ';
  27. }
  28. }
  29. ADD our custom category rewrite rules
  30. Add_filter (' category_rewrite_rules ', ' no_category_base_rewrite_rules ');
  31. function No_category_base_rewrite_rules ($category _rewrite) {
  32. Var_dump ($category _rewrite); For debugging
  33. $category _rewrite = Array ();
  34. $categories = get_categories (Array (' hide_empty ' = = False));
  35. foreach ($categories as $category) {
  36. $category _nicename = slug, $category;
  37. if ($category, parent = = $category cat_id)//recursive recursion
  38. $category, parent = 0;
  39. ElseIf ($category, parent! = 0)
  40. $category _nicename = Get_category_parents ($category, parent, false, '/', true). $category _nicename;
  41. $category _rewrite[' ('. $category _nicename. ')/(?: feed/)? (Feed|rdf|rss|rss2|atom)/?$ '] = ' index.php?category_name= $matches [1]&feed= $matches [2] ';
  42. $category _rewrite[' ('. $category _nicename. ')/page/? ([0-9]{1,})/?$ '] = ' index.php?category_name= $matches [1]&paged= $matches [2] ';
  43. $category _rewrite[' ('. $category _nicename. ')/?$ '] = ' index.php?category_name= $matches [1] ';
  44. }
  45. Redirect support from the old Category Base
  46. Global $WP _rewrite;
  47. $old _category_base = get_option (' category_base ')? Get_option (' category_base '): ' Category ';
  48. $old _category_base = Trim ($old _category_base, '/');
  49. $category _rewrite[$old _category_base. '/(. *) $ '] = ' index.php?category_redirect= $matches [1] ';
  50. Var_dump ($category _rewrite); For debugging
  51. return $category _rewrite;
  52. }
  53. Add ' category_redirect ' query variable
  54. Add_filter (' Query_vars ', ' no_category_base_query_vars ');
  55. function No_category_base_query_vars ($public _query_vars) {
  56. $public _query_vars[] = ' category_redirect ';
  57. return $public _query_vars;
  58. }
  59. Redirect if ' Category_redirect ' is set
  60. Add_filter (' request ', ' no_category_base_request ');
  61. function No_category_base_request ($query _vars) {
  62. Print_r ($query _vars); For debugging
  63. if (isset ($query _vars[' Category_redirect ')) {
  64. $catlink = Trailingslashit (get_option (' Home)). User_trailingslashit ($query _vars[' category_redirect '], ' category ');
  65. Status_header (301);
  66. Header ("Location: $catlink");
  67. Exit ();
  68. }
  69. return $query _vars;
  70. }
Copy Code
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.