Making WordPress.org

Changeset 9186


Ignore:
Timestamp:
10/15/2019 10:08:12 PM (5 years ago)
Author:
coffee2code
Message:

Breathe: Output <link rel="canonical"> tag for homepage and its paginated views.

Props jonoaldersonwp, coffee2code.
Fixes #4545.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/themes/pub/wporg-breathe/functions.php

    r9062 r9186  
    4646}
    4747add_action( 'wp_head', __NAMESPACE__ . '\no_robots_search_results', 9 );
     48
     49
     50/**
     51 * Outputs `<link rel="canonical">` tags where needed.
     52 */
     53function rel_canonical() {
     54    $canonical = false;
     55
     56    if ( is_front_page() ) {
     57        $canonical = home_url( '/' );
     58    }
     59
     60    if ( $canonical && get_query_var( 'paged' ) > 1 ) {
     61        $canonical .= 'page/' . (int) get_query_var( 'paged' ) . '/';
     62    }
     63
     64    if ( $canonical ) {
     65        printf( '<link rel="canonical" href="%s">' . "\n", esc_url( $canonical ) );
     66    }
     67}
     68add_action( 'wp_head', __NAMESPACE__ . '\rel_canonical' );
    4869
    4970/**
Note: See TracChangeset for help on using the changeset viewer.