Heim > Backend-Entwicklung > PHP-Tutorial > wordpress分类目录去除category

wordpress分类目录去除category

WBOY
Freigeben: 2016-07-25 08:47:44
Original
1322 Leute haben es durchsucht
演示效果: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', ' // For pre-3.4 support
  23. $wp_rewrite -> extra_permastructs['category'][0] = '%category%';
  24. } else {
  25. $wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
  26. }
  27. }
  28. // Add our custom category rewrite rules
  29. add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
  30. function no_category_base_rewrite_rules($category_rewrite) {
  31. //var_dump($category_rewrite); // For Debugging
  32. $category_rewrite = array();
  33. $categories = get_categories(array('hide_empty' => false));
  34. foreach ($categories as $category) {
  35. $category_nicename = $category -> slug;
  36. if ($category -> parent == $category -> cat_ID)// recursive recursion
  37. $category -> parent = 0;
  38. elseif ($category -> parent != 0)
  39. $category_nicename = get_category_parents($category -> parent, false, '/', true) . $category_nicename;
  40. $category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
  41. $category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
  42. $category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
  43. }
  44. // Redirect support from Old Category Base
  45. global $wp_rewrite;
  46. $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
  47. $old_category_base = trim($old_category_base, '/');
  48. $category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
  49. //var_dump($category_rewrite); // For Debugging
  50. return $category_rewrite;
  51. }
  52. // Add 'category_redirect' query variable
  53. add_filter('query_vars', 'no_category_base_query_vars');
  54. function no_category_base_query_vars($public_query_vars) {
  55. $public_query_vars[] = 'category_redirect';
  56. return $public_query_vars;
  57. }
  58. // Redirect if 'category_redirect' is set
  59. add_filter('request', 'no_category_base_request');
  60. function no_category_base_request($query_vars) {
  61. //print_r($query_vars); // For Debugging
  62. if (isset($query_vars['category_redirect'])) {
  63. $catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
  64. status_header(301);
  65. header("Location: $catlink");
  66. exit();
  67. }
  68. return $query_vars;
  69. }
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage