奥多码,点击查看详情 97CDN云盾,点击查看详情

wordpress教程:修正设置伪静态后文章分页链接

       
广告2021-06-03到期2021-07-03广告2021-06-03到期2021-07-03
       
广告2021-06-03到期2021-07-03广告2021-06-03到期2021-07-03

wordpress教程:修正设置伪静态后文章分页链接

我们通常将WordPress固定链接设为/%postname%.html或者/%post_id%.html,可以让页面看起来像静态页,但当文章有分页时,分页链接会变得奇怪,比如:

/morning-paper-news.html/3

/132.html/2

html既然是后缀就应该一直在最后,来自solagirl的《用.html作为url后缀时的分页链接问题》一文,为我们提供了解决办法。

不过原代码只提供了/%postname%.html的修改方法。

本文提供一下/%post_id%.html的修改方法。

将下面代码添加到当前主题 functions.php中:

  1. //适合/%post_id%.html分页链接修正
  2. classRewrite_Inner_Page_Links_id{
  3. var$separator;
  4. function__construct(){
  5. $this->separator=‘/page-‘;
  6. if(!is_admin()||defined(‘DOING_AJAX’)):
  7. add_filter(‘wp_link_pages_link’,array($this,‘inner_page_link_format’),10,2);
  8. add_filter(‘get_comments_pagenum_link’,array($this,‘comment_page_link_format’));
  9. add_filter(‘redirect_canonical’,array($this,‘cancel_redirect_for_paged_posts’),10,2);
  10. endif;
  11. if(is_admin()):
  12. add_filter(‘rewrite_rules_array’,array($this,‘pagelink_rewrite_rules’));
  13. register_activation_hook(__FILE__,array($this,‘flush_rewrite_rules’));
  14. register_deactivation_hook(__FILE__,array($this,‘flush_rewrite_rules’));
  15. endif;
  16. }
  17. functionflush_rewrite_rules(){
  18. flush_rewrite_rules();
  19. }
  20. //修改post分页链接的格式
  21. functioninner_page_link_format($link,$number){
  22. if($number>1){
  23. if(preg_match(‘%<ahref=“.*.html/d*”%’,$link)){
  24. $link=preg_replace(“%(.html)/(d*)%”,$this->separator.“$2$1”,$link);
  25. }
  26. }
  27. return$link;
  28. }
  29. //为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录
  30. functionpagelink_rewrite_rules($rules){
  31. foreach($rulesas$rule=>$rewrite){
  32. if($rule==‘([0-9]+).html(/[0-9]+)?/?$’){
  33. unset($rules[$rule]);
  34. }
  35. }
  36. $new_rule[‘([0-9]+)(‘.$this->separator.'([0-9]+))?.html/?$’]=‘index.php?p=$matches[1]&page=$matches[3]’;
  37. return$new_rule+$rules;
  38. }
  39. //禁止WordPress将页面分页链接跳转到原来的格式
  40. functioncancel_redirect_for_paged_posts($redirect_url,$requested_url){
  41. global$wp_query;
  42. if(is_single()&&$wp_query->get(‘page’)>1){
  43. returnfalse;
  44. }
  45. returntrue;
  46. }
  47. }
  48. newRewrite_Inner_Page_Links_id();

添加代码后,需要保存一下固定链接设置。

之后再次打开文章分页链接,会变成类似的:

/morning-paper-news/page-2.html

/132/page-2.html

注:上述代码并没有评论分页的链接修正,本人无此刚需未做研究。

其它固定链接形式,需要安装rewrite rules inspector插件查看链接正则写法并修改上述代码。

附:适合/%postname%.html链接形式原代码:

  1. /*
  2. *修复.html分页链接
  3. */
  4. classRewrite_Inner_Page_Links{
  5. var$separator;
  6. function__construct(){
  7. $this->separator=‘/page-‘;
  8. if(!is_admin()||defined(‘DOING_AJAX’)):
  9. add_filter(‘wp_link_pages_link’,array($this,‘inner_page_link_format’),10,2);//forinnerpages
  10. add_filter(‘get_comments_pagenum_link’,array($this,‘comment_page_link_format’));
  11. add_filter(‘redirect_canonical’,array($this,‘cancel_redirect_for_paged_posts’),10,2);
  12. endif;
  13. if(is_admin()):
  14. add_filter(‘rewrite_rules_array’,array($this,‘pagelink_rewrite_rules’));
  15. register_activation_hook(__FILE__,array($this,‘flush_rewrite_rules’));
  16. register_deactivation_hook(__FILE__,array($this,‘flush_rewrite_rules’));
  17. endif;
  18. }
  19. functionflush_rewrite_rules(){
  20. flush_rewrite_rules();
  21. }
  22. /**
  23. *修改post分页链接的格式
  24. *@paramstring$link
  25. *@paramint$number
  26. *@returnstring
  27. */
  28. functioninner_page_link_format($link,$number){
  29. if($number>1){
  30. if(preg_match(‘%<ahref=“.*.html/d*”%’,$link)){
  31. $link=preg_replace(“%(.html)/(d*)%”,$this->separator.“$2$1”,$link);
  32. }
  33. }
  34. return$link;
  35. }
  36. /**
  37. *修改评论分页链接
  38. *@paramstring$result
  39. *@returnstring
  40. */
  41. functioncomment_page_link_format($result){
  42. //Fromhello-world.html/comment-page-1#commentstohello-world/comment-page-1.html#comments
  43. if(strpos($result,‘.html/’)!==false){
  44. $result=preg_replace(‘=([^/]+)(.html)/comment-page-([0-9]{1,})=’,“$1/comment-page-$3$2”,$result);
  45. }
  46. return$result;
  47. }
  48. /**
  49. *为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录
  50. *
  51. *访问原始链接将返回404
  52. *@paramarray$rules
  53. *@returnarray
  54. */
  55. functionpagelink_rewrite_rules($rules){
  56. foreach($rulesas$rule=>$rewrite){
  57. if($rule==‘([^/]+).html(/[0-9]+)?/?$’||$rule==‘([^/]+).html/comment-page-([0-9]{1,})/?$’){
  58. unset($rules[$rule]);
  59. }
  60. }
  61. $new_rule[‘([^/]+)(‘.$this-&gt;separator.'([0-9]+))?.html/?$’]=‘index.php?name=$matches[1]&page=$matches[3]’;
  62. $new_rule[‘([^/]+)/comment-page-([0-9]{1,}).html(#[^s])?$’]=‘index.php?name=$matches[1]&amp;cpage=$matches[2]’;
  63. return$new_rule+$rules;
  64. }
  65. /**
  66. *禁止WordPress将页面分页链接跳转到原来的格式
  67. *@paramstring$redirect_url
  68. *@paramstring$requested_url
  69. *@returnbool
  70. */
  71. functioncancel_redirect_for_paged_posts($redirect_url,$requested_url){
  72. global$wp_query;
  73. if(is_single()&&$wp_query->get(‘page’)>1){
  74. returnfalse;
  75. }
  76. returntrue;
  77. }
  78. }
  79. newRewrite_Inner_Page_Links();
本文由【好易之】整理自网络!
原创文章,作者:【好易之】如转载请注明出处:https://www.zhengjiaxi.com/zxwd/jswd/69873.html
如有侵权,请邮件联系 aoduoye@qq.com 删除。
本站发布的文章及附件仅限用于学习和研究目的;不得将上述内容用于商业或非法用途,否则后果请用户自负。
本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。
如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。如有侵权请邮件与我们联系处理。
(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
好易之的头像好易之站长
上一篇 2020-05-27 00:38
下一篇 2020-05-27 00:38

相关推荐

发表回复

登录后才能评论

联系我们

400-800-8888

在线咨询:点击这里给我发消息

 

工作时间:周一至周五,9:30-18:30,节假日休息

关注公众号
请查看头部文章来源地址!本站所有内容均为互联网收集整理和网友上传。仅限于学习研究,切勿用于商业用途。否则由此引发的法律纠纷及连带责任本站概不承担。