admin管理员组文章数量:1434916
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' );
}
} );
本文标签: url rewritingHaving trouble with using addrewriterule and pagination
版权声明:本文标题:url rewriting - Having trouble with using add_rewrite_rule and pagination 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745645023a2668084.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论