在WordPress帖子中指定自定义规范URL [英] Specify custom canonical URL in WordPress Post

查看:187
本文介绍了在WordPress帖子中指定自定义规范URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们运营着1000多个网站,其中大多数网站都是为特定体育赛事而制作的.目前,我们的作家专门针对独特的内容写信给他们.

We operate 1000's of websites, most of them are are made for specific sports events. Currently we have our writers write to all of them specifically for unique content.

但是,我们有2个主要站点,涵盖了所有垂直领域的事件;并且我们希望从这些主要网站开始将内容联合到这些微型网站.

However we have 2 major sites that cover all events in their verticals; and we would like to start syndicating content to the minisites, from these major sites.

要在Google眼中保持最佳做法,我们必须通过rel = canonical标签指定文章的原始来源-但是我们当前的插件AIOSEO(多合一SEO)不支持在上指定规范标签帖子或页面基础.

To maintain best practices in Google's eyes, we would have to specify the original source of the article via the rel=canonical tag - however our current plugin AIOSEO (All-in-One SEO) doesn't support specifying canonical tags on a post, or page basis.

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

Is there a way to create such a function?

推荐答案

请使用以下代码:

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' );

这篇关于在WordPress帖子中指定自定义规范URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆