修改wordpress内容分页伪静态URL
上图是我的文章伪静态URL规则。
今天偶尔发布一篇带有分页的文章结果发现分页的URL是https://www.nnbbxx.net/post-4927.html/2这样。
所以去网络上搜集资料。在花了几分钟时间里,拿着收集来的代码来改成自己的URL规则。
因为参考了很多人的所以不放原文连接了。
//解析url的钩子 add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules'); function add_custom_post_rewrite_rules($rules) { $custom_rules = array('(\d+)_(\d+)\.html$' => 'index.php?p=$matches[1]&page=$matches[2]',); 你想改的样子 $rules = array_merge($custom_rules, $rules); return $rules; } //设置url钩子 add_filter('wp_link_pages_link', 'post_custom_rewrite_url'); function post_custom_rewrite_url($output){ $preg = "/(.*)\/(\d+)\.html\/(\d+)/"; 你目前的分页链接 $output = preg_replace($preg, "$1/$2_$3.html", $output); 要匹配的连接 return $output; } //不许跳转 add_filter( 'redirect_canonical', 'post_custom_redirect_url'); function post_custom_redirect_url($output){ return false; }
https://www.nnbbxx.net/post-4927.html 我的URL 我要把分页改成 https://www.nnbbxx.net/post-4927_2.html
所以我的是这样写的:如下
$custom_rules = array('post-(\d+)_(\d+)\.html$' => 'index.php?p=$matches[1]&page=$matches[2]',);
$preg = "/(.*)\/post-(\d+)\.html\/(\d+)/"; $output = preg_replace($preg, "$1/post-$2_$3.html", $output);
保存到函数文件里,functions.php。
保存之后打开文章点击分页连接变成404了的话,就去后台的固定链接里重新保存一次。什么不要动不改进入固定链接页面直接点击保存,在返回文章页点击或刷新分页页面。
大功告成。
比如我的:https://www.nnbbxx.net/post-4927_3.html
https://www.nnbbxx.net/post-4927_2.html
https://www.nnbbxx.net/post-4927.html