给WordPress博客添加meta

Meta Tags in WordPress

As a search engine prowls your site, it gathers information from the title, headings, content, and Meta Tags such as description or keywords. It compares the words within each of these sections and "ranks" the site dependent upon how well the information matches. We have more information on how to maximize your meta tag references below.

It is important for website developers to understand that a default installation of WordPress does not contain the description and keywords meta tag data. Meta tags can be added manually, through changes to the Theme template files or through WordPress Plugins.

Meta Tags were created early on to provide concise information about a website. Meta tags list information about the web page, such as the author, keywords, description, type of document, copyright, and other core information.

meta tag for description示例:

<meta name="description" content="This is the
description sentence or short paragraph about
the article or post." />

常用meta tags示例:

<meta name="resource-type" content="document" />
<meta http-equiv="content-type" content="text/html; charset=US-ASCII" />
<meta http-equiv="content-language" content="en-us" />
<meta http-equiv="author" content="Harriet Smith" />
<meta http-equiv="contact" content="harrietsmith@harrietsmith.us" />
<meta name="copyright" content="Copyright (c)1997-2004
Harriet Smith. All Rights Reserved." />
<meta name="description" content="Story about my dog
giving birth to puppies." />
<meta name="keywords" content="stories, tales, harriet, smith,
harriet smith, storytelling, day, life, dog, birth, puppies, happy" />

以上内容来自WordPress官方。


当然,我们可以通过WordPress插件类如All in One SEO Pack实现自动生成metatags,但是插件加载需要耗费一定的时间,我们可以通过代码实现metatags的实现。

<?if (is_home()){

$description = "博客描述";

$keywords = "博客关键字";

} elseif (is_single()){

if ($post->post_excerpt) {

   $description     = $post->post_excerpt;

} else {

   $description = substr(strip_tags($post->post_content),0,270);

}

$keywords = "";      
$tags = wp_get_post_tags($post->ID);

foreach ($tags as $tag ) {

$keywords = $keywords . $tag->name . ", ";

}

}

?>

<meta name="keywords" content="<?=$keywords?>" />

<meta name="description" content="<?=$description?>" />

编辑WordPress主题的header.php文件,将上面的代码复制到<head></head>之间,同时将“博客描述”“博客关键字”替换成你自己的设置。

通过is_home和is_single实现对博客首页和文章页的判断,上面代码中的270是description摘要长度。


本文地址: 给WordPress博客添加meta

本站采用署名-非商业性使用-相同方式共享 3.0 许可协议,转载请注明转自Simnovo


6 条评论

发表评论