WordPress.org

Making WordPress.org

Changeset 9319


Ignore:
Timestamp:
12/10/2019 11:40:41 PM (10 months ago)
Author:
coffee2code
Message:

Developer: Output <link rel="canonical"> tag taxonomies and post type archives.

Props jonoaldersonwp, coffee2code.
Fixes #4712.

File:
1 edited

Legend:

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

    r9085 r9319  
    449449}
    450450add_action( 'wp_head', __NAMESPACE__ . '\add_meta_description_for_summary' );
     451
     452/**
     453 * Outputs `<link rel="canonical">` tags where appropriate.
     454 */
     455function rel_canonical() {
     456    $canonical = false;
     457    $queried_object = get_queried_object();
     458
     459    if ( is_tax() || is_tag() || is_category() ) {
     460        $canonical = get_term_link( $queried_object );
     461    } elseif ( is_post_type_archive() ) {
     462        $canonical = get_post_type_archive_link( $queried_object->name );
     463    }
     464
     465    if ( $canonical && get_query_var( 'paged' ) > 1 ) {
     466        $canonical .= 'page/' . (int) get_query_var( 'paged' ) . '/';
     467    }
     468
     469    if ( $canonical ) {
     470        printf( '<link rel="canonical" href="%s">' . "\n", esc_url( $canonical ) );
     471    }
     472}
     473add_action( 'wp_head', __NAMESPACE__ . '\rel_canonical', 9 );
     474
Note: See TracChangeset for help on using the changeset viewer.