Home  >  Article  >  Backend Development  >  remove category from wordpress category directory

remove category from wordpress category directory

WBOY
WBOYOriginal
2016-07-25 08:47:441306browse
演示效果:http://houjinzhe.com/news/
  1. /*
  2. *去除分类标志代码
  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 = $category -> slug;
  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 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. }
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn