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

时间:2013-09-10 04:55:20

标签: php wordpress seo canonical-link rel

我们经营着1000多个网站,其中大部分是针对特定体育赛事而制作的。目前,我们的作者专门针对所有作者写了独特的内容。

然而,我们有2个主要网站,涵盖垂直行业的所有活动;我们希望从这些主要网站开始向minisites联合内容。

为了保持Google眼中的最佳做法,我们必须通过rel = canonical标签指定文章的原始来源 - 但是我们当前的插件AIOSEO(All-in-One SEO)并没有这样做。 t支持在帖子或页面上指定规范标签。

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

2 个答案:

答案 0 :(得分: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' );

答案 1 :(得分:2)

我们还可以为元关键字,元描述和元标题添加此内容

// function to insert All-in-One SEO Pack keywords
function keyword_insert() {
   global $post; // VERY important!

   // Retrieve keyword meta data from the SEO Pack
   $seokeywords = stripslashes(get_post_meta($post->ID, '_aioseop_keywords', true));

   // Default keywords in case none are specified for the page
   if (empty($seokeywords)) $seokeywords = "Homestar Runner, Strong Bad, The Cheat";

   // Output the html code
   $seokeyword_block = "<meta name=\"keywords\" content=\"".$seokeywords."\"/>\n";
   echo $seokeyword_block;
}

// function to insert All-in-One SEO Pack description
function description_insert() {
   global $post; // VERY important!

   // Retrieve description meta data from the SEO Pack
   $seodesc = stripslashes(get_post_meta($post->ID, '_aioseop_description', true));

   // Default description in case none is specified for the page
   if (empty($seodesc)) $seodesc = "Oh! I am Homestar, and This is A Website!";

   // Output the html code
   $seodesc_block = "<meta name=\"description\" content=\"".$seodesc."\"/>\n";
   echo $seodesc_block;
}

function title_insert() {
   global $post; // VERY important!

   // Retrieve title meta data from the SEO Pack
   $seotitle = stripslashes(get_post_meta($post->ID, '_aioseop_title', true));

   // Default description in case none is specified for the page
   if (empty($seotitle)) $seotitle = "";

   // Output the html code
   $seotitle_block = "<title>".$seotitle."</title><meta name=\"title\" content=\"".$seotitle."\"/>\n";
   echo $seotitle_block;
}

Please check this URL for reference