也想出現在這里?聯系我們

wordpress教程:免插件實現No Category Base效果:去除分類標志

2014.08.08 wordpress教程
  • 文章介紹
  • 升級版本
  • 評價&建議

天要介紹的是No Category Base插件,百度貌似對三級目錄的收錄不喜,它的功能就是刪除分類目錄鏈接中/category/?那有些多余的字樣。若單單為了SBO而再多裝一個插件實在不值,不如……把它集成到主題中?
加入該代碼至主題文件functions.php:

  1. //去除分類標志代碼
  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. //?Add?'category_redirect'?query?variable
  52. add_filter('query_vars',?'no_category_base_query_vars');
  53. function?no_category_base_query_vars($public_query_vars)?{
  54. ????$public_query_vars[]?=?'category_redirect';
  55. ????return?$public_query_vars;
  56. }
  57. //?Redirect?if?'category_redirect'?is?set
  58. add_filter('request',?'no_category_base_request');
  59. function?no_category_base_request($query_vars)?{
  60. ????//print_r($query_vars);?//?For?Debugging
  61. ????if?(isset($query_vars['category_redirect']))?{
  62. ????????$catlink?=?trailingslashit(get_option('home'))?.?user_trailingslashit($query_vars['category_redirect'],?'category');
  63. ????????status_header(301);
  64. ????????header("Location:?$catlink");
  65. ????????exit();
  66. ????}
  67. ????return?$query_vars;
  68. }

方法很簡單,但是首先至少得熟悉wordpress的固定連接的使用常識了!

下面我把文件上傳到百度云,以免大家復制出錯!

有用0
  • 2014.08.08初次和大家見面了!

已擁有2人對該主題的建議

  1. ?? 為嘛修改后點文章找不到文章了

    2014-12-03 下午 5:52 [回復]
    • 你好似傻一看就是假的

      2019-05-17 下午 3:14 [回復]

發表評論

還能輸入240個字

Hi, 歡迎加入Wordpress技術交流群,帶你裝逼帶你飛!

我要入群
也想出現在這里?聯系我們
wordpress加速

我來推薦一個更牛逼的給你看看?

  • 猛戳我吧
夫妇野外交换HD高清版,免费A片在线观看,亚洲精品国产自在现线,中国老妇人60OLDMANTV