Canonical: Redirect paged requests for a static page assigned as the "Posts page".

This avoids displaying duplicate content of the home page under different URLs with appended page numbers.

This change only affects the `<!--nextpage-->` pagination (`page` query variable) and not the regular multiple posts pagination (`paged` query variable).

The posts page does not support the `<!--nextpage-->` pagination, so requests for invalid page numbers should be redirected to the page permalink, applying the logic previously implemented for single posts or pages.

Follow-up to [34492], [47727].

Props jeremyfelt, sachit.tandukar, SergeyBiryukov.
Fixes #45337. See #40773, #28081, #11694.

git-svn-id: https://develop.svn.wordpress.org/trunk@47760 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2020-05-04 10:40:06 +00:00
parent 54c8470bd6
commit de14ea86bf
4 changed files with 41 additions and 20 deletions

View File

@@ -58,8 +58,13 @@ class Tests_Canonical_PageOnFront extends WP_Canonical_UnitTestCase {
// The page designated as the front page should redirect to the front of the site.
array( '/front-page/', '/' ),
// The front page supports the <!--nextpage--> pagination.
array( '/front-page/2/', '/page/2/' ),
array( '/front-page/?page=2', '/page/2/' ),
// The posts page does not support the <!--nextpage--> pagination.
array( '/blog-page/2/', '/blog-page/' ),
array( '/blog-page/?page=2', '/blog-page/' ),
// The posts page supports regular pagination.
array( '/blog-page/?paged=2', '/blog-page/page/2/' ),
);
}

View File

@@ -6,7 +6,7 @@
*/
class Tests_Canonical_Paged extends WP_Canonical_UnitTestCase {
function test_nextpage() {
function test_redirect_canonical_with_nextpage_pagination() {
$para = 'This is a paragraph.
This is a paragraph.
This is a paragraph.';
@@ -19,9 +19,12 @@ class Tests_Canonical_Paged extends WP_Canonical_UnitTestCase {
)
);
$link = parse_url( get_permalink( $post_id ), PHP_URL_PATH );
$paged = $link . '4/';
$link = parse_url( get_permalink( $post_id ), PHP_URL_PATH );
$this->assertCanonical( $paged, $link );
// Existing page should be displayed as is.
$this->assertCanonical( $link . '3/', $link . '3/' );
// Non-existing page should redirect to the permalink.
$this->assertCanonical( $link . '4/', $link );
}
}