- 加入我的QQ群
- 关注我的百家号
扫描下面的二维码,“关注”我的百家号。
第一步、在wordpress主题的functions.php文件中添加文章描述函数代码:
//文章描述函数代码:
function fanly_excerpt($len=220){
if ( is_single() || is_page() ){
global $post;
if ($post->post_excerpt) { //如果在后台文章编辑页面添加了描述,就直接调用这个描述;
$excerpt = $post->post_excerpt;
} else { //如果没有填写描述,就调用文章内容,截取开头的内容作为描述。
if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$result)){
$post_content = $result['1'];
} else {
$post_content_r = explode("\n",trim(strip_tags($post->post_content)));
$post_content = $post_content_r['0'];
}
$excerpt = preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,0}'.'((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$len.'}).*#s','$1',$post_content);
}
return str_replace(array("\r\n", "\r", "\n"), "", $excerpt);
}
}
二、在wordpress主题的文章页头部添加调用描述代码。
<?php
if(is_single() || is_page()){
$description = fanly_excerpt(220);
$keywords = "";
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$keywords = $keywords . $tag->name . ", ";
}
}
?>
<meta name="keywords" content="<?php echo $keywords ?>" />
<meta name="description" content='<?php echo strip_tags(trim($description)); ?>' />
220个字符比较符合seo,是吧