We have a section on the site, such as:
www.example/for-sale/category-name/
for-sale
is a WP page, however category-name
is dynamic and is what I wrote the below rewrite rule for.
add_rewrite_rule('for-sale\/([a-z-]+)\/?$', 'index.php?pagename=for-sale&search_slug=$matches[1]', 'top');
However, problems arise when the user needs to use the page
query var, if I try going to:
www.example/for-sale/category-name/?page=2
it get's rewritten to:
www.example/for-sale/2/
So completely strips out the category slug.
Is there a way I can get this to work?
...or preferably I would like to be able to use something like this:
www.example/for-sale/category-name/2/
So I tried adding this additional rewrite rule:
add_rewrite_rule('for-sale\/([a-z-]+)\/([0-9])+\/?$', 'index.php?pagename=for-sale&search_slug=$matches[1]&page=$matches[2]', 'top');
..but again, it redirected back to:
www.example/for-sale/2/
What am I missing here?
We have a section on the site, such as:
www.example/for-sale/category-name/
for-sale
is a WP page, however category-name
is dynamic and is what I wrote the below rewrite rule for.
add_rewrite_rule('for-sale\/([a-z-]+)\/?$', 'index.php?pagename=for-sale&search_slug=$matches[1]', 'top');
However, problems arise when the user needs to use the page
query var, if I try going to:
www.example/for-sale/category-name/?page=2
it get's rewritten to:
www.example/for-sale/2/
So completely strips out the category slug.
Is there a way I can get this to work?
...or preferably I would like to be able to use something like this:
www.example/for-sale/category-name/2/
So I tried adding this additional rewrite rule:
add_rewrite_rule('for-sale\/([a-z-]+)\/([0-9])+\/?$', 'index.php?pagename=for-sale&search_slug=$matches[1]&page=$matches[2]', 'top');
..but again, it redirected back to:
www.example/for-sale/2/
What am I missing here?
Share Improve this question asked Mar 29, 2019 at 22:45 BrettBrett 4145 silver badges25 bronze badges1 Answer
Reset to default 4Your rewrite rules are good, but the redirect happens because WordPress applies canonical redirect via its redirect_canonical()
function which is hooked to template_redirect
.
And you can cancel the redirect via the parse_request
action, like so, where we check if the matched rewrite rule is the one you set when you call add_rewrite_rule()
and if it is, then cancel the redirect by "unhooking" redirect_canonical()
from the template_redirect
action:
add_action( 'parse_request', function( $wp ){
if ( 'for-sale\/([a-z-]+)\/([0-9])+\/?$' === $wp->matched_rule ) {
remove_action( 'template_redirect', 'redirect_canonical' );
}
} );
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745645024a4637919.html
评论列表(0条)