注意:解决方法是在justnews主题下设置的,如果是其他主题,请参照着修改
最后效果为
将.html/2替换为_2.html
1、打开wp-content/themes/justnews/themer/functions/pagination.php
搜索
$url = _wp_link_page($i);
在下面添加如下代码
$url = preg_replace("%(\.html)/(\d*)%", '_' . "$2$1", $url);
2、打开wp-content/themes/justnews/functions.php
最后面添加下面的代码
class rewrite_inner_page_links
{
var $separator;
var $post_rule;
function __construct()
{
$this->separator = '—';
// (.+?)/([^/]+).html(/[0-9]+)?/?
$this->post_rule = '(.+?)/([^/]+)(' . $this->separator . '([0-9]+))+.html/?$';
if (!is_admin() || defined('doing_ajax')) :
add_filter('wp_link_pages_link', array($this, 'inner_page_link_format'), 10, 2); // for inner pages
add_filter('redirect_canonical', array($this, 'cancel_redirect_for_paged_posts'), 10, 2);
endif;
if (is_admin()) :
add_filter('rewrite_rules_array', array($this, 'pagelink_rewrite_rules'));
endif;
}
/**
* 修改post分页链接的格式
* @param string $link
* @param int $number
* @return string
*/
function inner_page_link_format($link, $number)
{
if ($number > 1)
{
if (preg_match('%<a href=".*\.html/\d*"%', $link))
{
$link = preg_replace("%(\.html)/(\d*)%", $this->separator . "$2$1", $link);
}
}
return $link;
}
/**
* 为新的链接格式增加重定向规则,移除原始分页链接的重定向规则,防止重复收录
*
* 访问原始链接将返回404
* @param array $rules
* @return array
*/
function pagelink_rewrite_rules($rules)
{
$new_rule[$this->post_rule] = 'index.php?name=$matches[2]&page=$matches[4]';
return $new_rule + $rules;
}
/**
* 禁止wordpress将页面分页链接跳转到原来的格式
* @param string $redirect_url
* @param string $requested_url
* @return bool
*/
function cancel_redirect_for_paged_posts($redirect_url, $requested_url)
{
global $wp_query;
if (is_single() && $wp_query->get('page') > 1)
{
return false;
}
return true;
}
}
new rewrite_inner_page_links();
3、打开wp-content/themes/justnews/themer/functions/seo.php
搜索
if ( 0 !== $id && $url = wp_get_canonical_url( $id )) {
在下面添加代码
$url = preg_replace("%(\.html)/(\d*)%", '_' . "$2$1", $url);
4、添加伪静态规则
rewrite ^/(.*)/([0-9]+)_([0-9]+).html$ /?p=$2&page=$3 last;
到此为止。
发布者:彬彬笔记,转载请注明出处:https://www.binbinbiji.com/wordpress/3129.html