Links on the page I use are progressing as follows:
The code I use for Functions.php
add_action( 'init', 'wpse316713_pagination_base_rewrite_rule' );
function wpse316713_pagination_base_rewrite_rule() {
global $wp_rewrite;
$wp_rewrite->pagination_base = 'image';
}
Reference link: How to change the link structure of the homepage?
What I'm trying to do is remove the "image" base. How can we change it to ?
Note:
$wp_rewrite->pagination_base = 'image';
If I remove the word "image" here, it's double flip. " // " shaped. This leads to an inability to display the page. Going like . How can we completely remove it and return it to only 1?
Links on the page I use are progressing as follows: http://example/image/1
The code I use for Functions.php
add_action( 'init', 'wpse316713_pagination_base_rewrite_rule' );
function wpse316713_pagination_base_rewrite_rule() {
global $wp_rewrite;
$wp_rewrite->pagination_base = 'image';
}
Reference link: How to change the link structure of the homepage?
What I'm trying to do is remove the "image" base. How can we change it to http://example/1?
Note:
$wp_rewrite->pagination_base = 'image';
If I remove the word "image" here, it's double flip. " // " shaped. This leads to an inability to display the page. Going like http://example/image/2/1. How can we completely remove it and return it to only 1?
Share Improve this question asked Dec 15, 2018 at 17:16 A. MullerA. Muller 439 bronze badges1 Answer
Reset to default 1To remove pagination prefix page
in home page you should:
- add a rewrite rule which will translate the new link format,
- prevent redirection to address containing
page
, - replace URLs in paging links.
By creating pagination links, WordPress appends query var paged
with mentioned prefix to url. Therefore, without modification, the links would look like example/3/page/3
.
Next thing is redirection. WP redirects incoming links to the proper URL based on the site url.Thanks to our rewrite rule, for the example/3
url, the results of the third page will be displayed (paged=3
). But WP check that correct url for paged=3
should be example/3/page/3
which is different from example/3
.
Therefore, a redirection will be made.
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;
}
The new link format can not be used if the permalink structure is set to /%post_id%
.