WordPress SEO Optimization: adding canonical tags to pure code

Adding canonical tag to a website is a very important step in SEO optimization. rel = "canonical" can solve the problem of weight dispersion caused by different URLs but repeated content. At present, Baidu, Google, Yahoo, Microsoft and other search engines have supported this tag.

Example demonstration

https://www.nicebrian.com/

In order to avoid the dispersion of the weight of the home page, we should tell the search engine through the rrel = "canonical" tag that the weight of these two pages is mainly concentrated in the first web site.

<link rel="canonical" href="https://www.nicebrian.com/" />

The specific method is to add the above tag to the / head tag of the two pages.

Add method

Share two ways to automatically add canonical tags to WordPress home page, category, tag and article page with pure code, and add any of the following code to WordPress theme functions.php File.

Method 1

//Add canonical tag in pure code to centralize page permissions
//https://www.nicebrian.com/12.html
remove_action( 'wp_head', 'rel_canonical' );
function cccitu_archive_link( $paged = true ) {
        $link = false;
        if ( is_front_page() ) {
                $link = home_url( '/' );
        } else if ( is_home() && "page" == get_option('show_on_front') ) {
                $link = get_permalink( get_option( 'page_for_posts' ) );
        } else if ( is_tax() || is_tag() || is_category() ) {
                $term = get_queried_object();
                $link = get_term_link( $term, $term->taxonomy );
        } else if ( is_post_type_archive() ) {
                $link = get_post_type_archive_link( get_post_type() );
        } else if ( is_author() ) {
                $link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') );
        } else if ( is_single() ) {
                $link = get_permalink( $id );
        } else if ( is_archive() ) {
                if ( is_date() ) {
                        if ( is_day() ) {
                                $link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') );
                        } else if ( is_month() ) {
                                $link = get_month_link( get_query_var('year'), get_query_var('monthnum') );
                        } else if ( is_year() ) {
                                $link = get_year_link( get_query_var('year') );
                        }
                }
        }
        if ( $paged && $link && get_query_var('paged') > 1 ) {
                global $wp_rewrite;
                if ( !$wp_rewrite->using_permalinks() ) {
                        $link = add_query_arg( 'paged', get_query_var('paged'), $link );
                } else {
                        $link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var('paged'), 'archive' );
                }
        }
        echo '<link rel="canonical" href="'.$link.'" />';
}
add_action('wp_head', 'cccitu_archive_link');

Method 2

remove_action( 'wp_head', 'rel_canonical' );
function cccitu_rel_canonical() {
    global $post;
    if (is_single() || is_page()) {
    echo "<link rel=\"canonical\" href=\"" . get_permalink( $post->ID ) . "\" />\n";
    }
    if (is_home()) {
    echo "<link rel=\"canonical\" href=\"".home_url("/")."\" />\n";
    }
    if (is_category() || is_category() && is_paged()) {
    echo "<link rel=\"canonical\" href=\"".get_category_link(get_query_var('cat'))."\" />\n";
    }
    if (is_tag() || is_tag() && is_paged()) {
    echo "<link rel=\"canonical\" href=\"".get_term_link(get_query_var('tag'), 'post_tag')."\" />\n";
    }
    if (is_search() || is_search() && is_paged()) {
    echo "<link rel=\"canonical\" href=\"".get_search_link(get_query_var('search'))."\" />\n";
    }
    if (is_author()) {
    echo "<link rel=\"canonical\" href=\"".get_option('home')."\" />\n";
    }
    if (is_date()) {
    echo "<link rel=\"canonical\" href=\"".get_option('home')."\" />\n";
    }
    }
add_action('wp_head', 'cccitu_rel_canonical');

After saving, refresh your page to see the canonical tag~

Original article, author: brian, if reprinted, please indicate the source:https://www.nicebrian.com/12.html

Leave a Reply

Your email address will not be published. Required fields are marked *