删除分页前缀的步骤page
在主页中,您应该:
添加重写规则,该规则将转换新的链接格式,防止重定向到包含page
,替换分页链接中的URL通过创建分页链接,WordPress附加查询变量paged
url的前缀。因此,如果不进行修改,链接将如下所示example.com/3/page/3
.
接下来就是重定向。WP根据站点URL将传入链接重定向到正确的URL。由于我们的重写规则example.com/3
url,将显示第三页的结果(paged=3
). 但是WP检查正确的urlpaged=3
应该是example.com/3/page/3
这与example.com/3
.因此,将进行重定向。
add_action( \'init\', \'se323035_structure\', 25 );
add_filter( \'paginate_links\', \'se323035_paginate_links\' );
add_filter( \'redirect_canonical\', \'se323035_redirect_canonical\', 20, 2 );
function se323035_structure()
{
global $wp_rewrite;
if ( $wp_rewrite->using_permalinks() )
add_rewrite_rule( \'([0-9]+)/?$\', \'index.php?&paged=$matches[1]\', \'top\');
}
function se323035_paginate_links( $link )
{
global $wp_rewrite;
if ( is_home() && $wp_rewrite->using_permalinks() ) {
$original = @parse_url( $link );
$home = @parse_url( home_url(\'/\') );
if ( ! isset( $original[\'path\'] ) )
$original[\'path\'] = \'\';
$original_path = $original[\'path\'];
$original_path_end = $original[\'path\'];
if ( isset($home[\'path\']) )
{
$home[\'path\'] = trim($home[\'path\']);
$home[\'path\'] = untrailingslashit($home[\'path\']);
if ( !empty($home[\'path\']) && $home[\'path\'] != \'/\' )
$original_path_end = str_replace( $home[\'path\'], \'\', $original[\'path\']);
}
//
// if original url match to our link structure do not redirect to \'/page/{number}\'
if ( preg_match( \'|^/?[0-9]+/?$|\', $original_path_end) ) {
$link = home_url( \'/\' );
}
elseif ( preg_match( "|/(\\d+/)?$wp_rewrite->pagination_base/\\d+/?$|", $original_path_end) )
$link = preg_replace( "|/(\\d+/)?$wp_rewrite->pagination_base/(\\d+/?)$|", \'/${2}\', $link );
}
return $link;
}
function se323035_redirect_canonical( $redirect_url, $requested_url )
{
global $wp_rewrite;
if ( is_home() && !empty($redirect_url) )
{
$original = @parse_url( $requested_url );
if ( ! isset( $original[\'path\'] ) )
$original[\'path\'] = \'\';
$original_path = $original[\'path\'];
$home = @parse_url( home_url(\'/\') );
if ( isset($home[\'path\']) )
{
$home[\'path\'] = trim($home[\'path\']);
$home[\'path\'] = untrailingslashit($home[\'path\']);
if ( !empty($home[\'path\']) && $home[\'path\'] != \'/\' )
$original_path = str_replace( $home[\'path\'], \'\', $original[\'path\']);
}
// if original url match to our link structure don\'t redirect to \'/page/{number}\'
if ( preg_match( \'|^/?[0-9]+/?$|\', $original_path) )
return \'\';
}
return $redirect_url;
}
如果permalink结构设置为,则无法使用新的链接格式
/%post_id%
.