Make WordPress Core


Ignore:
Timestamp:
05/04/2020 10:40:06 AM (5 years ago)
Author:
SergeyBiryukov
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/canonical.php

    r47759 r47760  
    187187        }
    188188
     189        // Strip off non-existing <!--nextpage--> links from single posts or pages.
     190        if ( get_query_var( 'page' ) ) {
     191            $post_id = 0;
     192
     193            if ( $wp_query->queried_object instanceof WP_Post ) {
     194                $post_id = $wp_query->queried_object->ID;
     195            } elseif ( $wp_query->post ) {
     196                $post_id = $wp_query->post->ID;
     197            }
     198
     199            $redirect_url = get_permalink( $post_id );
     200
     201            $redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
     202            $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
     203        }
     204
    189205        if ( ! $redirect_url ) {
    190206            $redirect_url = redirect_guess_404_permalink();
     
    197213                );
    198214            }
    199         }
    200 
    201         // Strip off non-existing page links from single posts or pages.
    202         if ( get_query_var( 'page' ) && $wp_query->post ) {
    203             $redirect['path']  = rtrim( $redirect['path'], (int) get_query_var( 'page' ) . '/' );
    204             $redirect['query'] = remove_query_arg( 'page', $redirect['query'] );
    205 
    206             $redirect_url = get_permalink( $wp_query->post->ID );
    207215        }
    208216    } elseif ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) {
Note: See TracChangeset for help on using the changeset viewer.