saber 酱的抱枕

Fly me to the moon

04/17
2019
软件

解决 WordPress 设置伪静态后分页问题

WordPress 后台里可以设置固定链接,达到伪静态的效果。但是文章里的分页就会出错了,比如第二页就会变成这样:

https://saber.love/postname.html/2

html 后面为什么要加个斜杠,它又不是目录,所以分页就会打不开。

在网上找到了解决办法,就是在主题的 functions.php 里加入以下代码:

//设置伪静态开始
//解析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;
}
//设置伪静态结束

解决 WordPress 设置伪静态后分页问题