My Computer · 2012/03/25 0

为WordPress添加description的一个小小改进

之前写过一篇《为WordPress手动添加keywords和description的修改方法》,按照这个方法可以灵活的添加keywords和description,但是如果文章摘要是图片、多媒体等这种情况是,恐怕description的内容就会有问题了。。。

特意对description的取值做了调整,当文章摘要是图片、多媒体时,需要在自定义栏目中添加一个名称为“description”的栏目,并填写对应值。这样既可以得到一个漂亮的摘要,也不耽误网页描述的内容。
elseif (is_single()) {
//如果文章的自定义栏目设置了description,则显示;否则显示220字节的文章摘要
$description1 = get_post_meta($post->ID, "description", true);
$description2 = mb_strimwidth(strip_tags(apply_filters('the_excerpt()', $post->post_excerpt)), 0, 220);
$description = $description1 ? $description1 : $description2;

$keywords ="";
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) { //遍历该文的所有标签
$keywords = $keywords . $tag->name . ","; //使用标签tags作为关键词
}
}

相关文章