wordpress中添加rel=canonical的代码方法

wordpress中添加rel=canonical的代码方法

分类: 网站代码 作者:网络 阅读量:

wordpress中添加rel="canonical"的代码方法

在 WordPress 2.9 之前,让 WordPress 博客支持 Canonical 标签是需要通过插件或者手工修改主题的 header.php 文件来实现。如在主题中加如下的代码:

<?php if(is_single()){?>
    <link rel="canonical" href="<?php echo get_permalink($post->ID);?>" />
<?php } ?>

在 WordPress 2.9 发布之后,WordPress 已经默认支持这一标签了,我们无需做任何动作,主题就支持这一标签。我们可以在 WordPress 的源文件 wp-includes/default-filters.php 看到如下的代码:

add_action( 'wp_head', 'rel_canonical' );

因此 WordPress 是调用 rel_canonical() 这个函数来输出 rel=”canonical” 标签的 HTML 代码。 如果你还想使用以前自己的方法,那么你可以通过下面代码屏蔽掉它:

remove_action( 'wp_head', 'rel_canonical' );

建议使用 WordPress 默认输出的 Canonical 标签。

如果WordPress无法自动添加rel="canonical",那采用以下纯代码为 WordPress 首页、分类、标签和文章页自动添加 canonical 标签。

首先在functions.php文件中添加分类目录分页链接获取函数,代码如下:

function v7v3_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_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' );
                }
        }
        return $link;
}

然后打开主题的头部文件(一般情况下为header.php)在其中添加以下代码:

<?php
if(is_home()) { ?>
<link rel="canonical" href="<?php echo v7v3_archive_link();?>"/>
<?php } ?>
<?php
if(is_category()) { ?>
<link rel="canonical" href="<?php echo v7v3_archive_link();?>"/>
<?php } ?>
<?php
if(is_single())  { ?>
<link rel="canonical" href="<?php the_permalink(); ?>"/>
<?php }?>
<?php
if(is_tag()) { ?>
<link rel="canonical" href="<?php echo v7v3_archive_link();?>"/>
<?php }?>