admin管理员组文章数量:1333437
I have this example link: /uncategorized/hello-world/
If I put 1 page break on the post content, redirects are as follows:
/uncategorized/hello-world/1
=> /uncategorized/hello-world/
redirect and is my ideal behavior.
/uncategorized/hello-world/2
=> /uncategorized/hello-world/2
no redirect, it's expected.
/uncategorized/hello-world/3
=> /uncategorized/hello-world/
redirect and is my ideal behavior.
BUT if I remove the page break on the post content, only /uncategorized/hello-world/1
works.
If page is 1, it removes the 1 in the url, but other than that it accepts the number.
/uncategorized/hello-world/1
=> /uncategorized/hello-world/
/uncategorized/hello-world/2
=> /uncategorized/hello-world/2
/uncategorized/hello-world/99
=> /uncategorized/hello-world/99
Is there any way to make it so any number should redirect as if there was no number at all.
/uncategorized/hello-world/1
=> /uncategorized/hello-world/
/uncategorized/hello-world/2
=> /uncategorized/hello-world/
/uncategorized/hello-world/99
=> /uncategorized/hello-world/
But only when there's no page break added on the post.
I have this example link: /uncategorized/hello-world/
If I put 1 page break on the post content, redirects are as follows:
/uncategorized/hello-world/1
=> /uncategorized/hello-world/
redirect and is my ideal behavior.
/uncategorized/hello-world/2
=> /uncategorized/hello-world/2
no redirect, it's expected.
/uncategorized/hello-world/3
=> /uncategorized/hello-world/
redirect and is my ideal behavior.
BUT if I remove the page break on the post content, only /uncategorized/hello-world/1
works.
If page is 1, it removes the 1 in the url, but other than that it accepts the number.
/uncategorized/hello-world/1
=> /uncategorized/hello-world/
/uncategorized/hello-world/2
=> /uncategorized/hello-world/2
/uncategorized/hello-world/99
=> /uncategorized/hello-world/99
Is there any way to make it so any number should redirect as if there was no number at all.
/uncategorized/hello-world/1
=> /uncategorized/hello-world/
/uncategorized/hello-world/2
=> /uncategorized/hello-world/
/uncategorized/hello-world/99
=> /uncategorized/hello-world/
But only when there's no page break added on the post.
- So if you have no page break /1 goes to / but /2 goes to /2 ? – mozboz Commented Jun 25, 2020 at 10:06
- @mozboz yes, whatever number. But if with page break, only those valid page numbers. Invalid page numbers got removed. – Reigel Gallarde Commented Jun 26, 2020 at 2:41
1 Answer
Reset to default 2 +350That's actually the default behavior when the post does not have any page breaks (i.e. the <!--nextpage-->
tag).
More specifically, in redirect_canonical()
, WordPress only performs a redirect if the post has the page break tag — and that the page number is invalid (e.g. requesting for page #3 when there are only 2 pages). Otherwise (i.e. no page breaks), those paginated URLs/requests are seen as valid and WordPress doesn't do any redirects except for the first page (e.g. /hello-world/1
) because the content is considered a duplicate of /hello-world
.
So for those "valid" requests, if you want to redirect them to the first page, you can use the pre_handle_404
hook like so:
add_filter( 'pre_handle_404', function ( $bool ) {
if ( is_singular( [ 'post', 'page', 'etc' ] ) && get_query_var( 'page' ) &&
false === strpos( get_queried_object()->post_content, '<!--nextpage-->' )
) {
wp_redirect( get_permalink( get_queried_object() ) );
exit;
}
return $bool;
} );
You can also use the wp
hook, but the pre_handle_404
seems better since it's called first:
add_action( 'wp', function ( $wp ) {
if ( is_singular( [ 'post', 'page', 'etc' ] ) && get_query_var( 'page' ) &&
false === strpos( get_queried_object()->post_content, '<!--nextpage-->' )
) {
wp_redirect( get_permalink( get_queried_object() ) );
exit;
}
} );
And I'm using is_singular()
to target singular requests like single Post (post type post
) pages.
本文标签: themesWeird post pagination url redirect
版权声明:本文标题:themes - Weird post pagination url redirect 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742313936a2451432.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论