在WordPress Post中指定自定义规范URL

SEO*_*Guy 6 php wordpress seo rel canonical-link

我们经营1000个网站,其中大部分是针对特定的体育赛事.目前,我们的编写者专门针对所有这些内容撰写独特内容.

但是,我们有2个主要网站,涵盖垂直行业的所有活动; 我们想从这些主要网站向迷你网站开始联合内容.

为了保持谷歌眼中的最佳实践,我们必须通过rel = canonical标签指定文章的原始来源 - 但是我们当前的插件AIOSEO(All-in-One SEO)不支持在帖子上指定规范标签,或页面基础.

有没有办法创建这样的功能?

Sha*_*tel 5

你可以请用这个代码:

function rel_canonical() {
    if ( !is_singular() )
        return;

    global $wp_the_query;
    if ( !$id = $wp_the_query->get_queried_object_id() )
        return;

    $link = get_permalink( $id );
    echo "<link rel='canonical' href='$link' />\n";
}

// A copy of rel_canonical but to allow an override on a custom tag
function rel_canonical_with_custom_tag_override()
{
    if( !is_singular() )
        return;

    global $wp_the_query;
    if( !$id = $wp_the_query->get_queried_object_id() )
        return;

    // check whether the current post has content in the "canonical_url" custom field
    $canonical_url = get_post_meta( $id, 'canonical_url', true );
    if( '' != $canonical_url )
    {
        // trailing slash functions copied from http://core.trac.wordpress.org/attachment/ticket/18660/canonical.6.patch
        $link = user_trailingslashit( trailingslashit( $canonical_url ) );
    }
    else
    {
        $link = get_permalink( $id );
    }
    echo "<link rel='canonical' href='" . esc_url( $link ) . "' />\n";
}

// remove the default WordPress canonical URL function
if( function_exists( 'rel_canonical' ) )
{
    remove_action( 'wp_head', 'rel_canonical' );
}
// replace the default WordPress canonical URL function with your own
add_action( 'wp_head', 'rel_canonical_with_custom_tag_override' );
Run Code Online (Sandbox Code Playgroud)