首页 > 后端开发 > php教程 > wordpress分类目录去除category

wordpress分类目录去除category

WBOY
发布: 2016-07-25 08:47:44
原创
1322 人浏览过
演示效果: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. }
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板