普通网友 2013-09-10 04:55
浏览 124

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

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.

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.

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?

  • 写回答

2条回答 默认 最新

  • douzhongjiu2263 2013-09-10 05:46
    关注

    can you please used this code :

    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' />
    ";
    }
    
    // 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 ) . "' />
    ";
    }
    
    // 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' );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画