Changeset 37685 for trunk/src/wp-includes/link-template.php
- Timestamp:
- 06/11/2016 01:30:23 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/link-template.php
r37564 r37685 3540 3540 3541 3541 /** 3542 * Returns the canonical URL for a post. 3543 * 3544 * When the post is the same as the current requested page the function will handle the 3545 * pagination arguments too. 3546 * 3547 * @since 4.6.0 3548 * 3549 * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`. 3550 * @return string|false The canonical URL, or false if the post does not exist or has not 3551 * been published yet. 3552 */ 3553 function wp_get_canonical_url( $post = null ) { 3554 $post = get_post( $post ); 3555 3556 if ( ! $post ) { 3557 return false; 3558 } 3559 3560 if ( 'publish' !== $post->post_status ) { 3561 return false; 3562 } 3563 3564 $canonical_url = get_permalink( $post ); 3565 3566 // If a canonical is being generated for the current page, make sure it has pagination if needed. 3567 if ( $post->ID === get_queried_object_id() ) { 3568 $page = get_query_var( 'page', 0 ); 3569 if ( $page >= 2 ) { 3570 if ( '' == get_option( 'permalink_structure' ) ) { 3571 $canonical_url = add_query_arg( 'page', $page, $canonical_url ); 3572 } else { 3573 $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' ); 3574 } 3575 } 3576 3577 $cpage = get_query_var( 'cpage', 0 ); 3578 if ( $cpage ) { 3579 $canonical_url = get_comments_pagenum_link( $cpage ); 3580 } 3581 } 3582 3583 /** 3584 * Filters the canonical URL for a post. 3585 * 3586 * @since 4.6.0 3587 * 3588 * @param string $string The post's canonical URL. 3589 * @param WP_Post $post Post object. 3590 */ 3591 return apply_filters( 'get_canonical_url', $canonical_url, $post ); 3592 } 3593 3594 /** 3542 3595 * Outputs rel=canonical for singular queries. 3543 3596 * 3544 3597 * @since 2.9.0 3598 * @since 4.6.0 Adjusted to use `wp_get_canonical_url()`. 3545 3599 */ 3546 3600 function rel_canonical() { … … 3549 3603 } 3550 3604 3551 if ( ! $id = get_queried_object_id() ) { 3605 $id = get_queried_object_id(); 3606 3607 if ( 0 === $id ) { 3552 3608 return; 3553 3609 } 3554 3610 3555 $url = get_permalink( $id ); 3556 3557 $page = get_query_var( 'page' ); 3558 if ( $page >= 2 ) { 3559 if ( '' == get_option( 'permalink_structure' ) ) { 3560 $url = add_query_arg( 'page', $page, $url ); 3561 } else { 3562 $url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' ); 3563 } 3564 } 3565 3566 $cpage = get_query_var( 'cpage' ); 3567 if ( $cpage ) { 3568 $url = get_comments_pagenum_link( $cpage ); 3569 } 3570 echo '<link rel="canonical" href="' . esc_url( $url ) . "\" />\n"; 3611 $url = wp_get_canonical_url( $id ); 3612 3613 if ( ! empty( $url ) ) { 3614 echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n"; 3615 } 3571 3616 } 3572 3617
Note: See TracChangeset
for help on using the changeset viewer.