欢迎访问 夜阑小雨 我的学习碎片档案,这里记录了我的学习内容和工作中经验,希望给您带去帮助。

wordpress如何获取关键字及描述方法

读书推荐 夜阑小雨 1768℃ 0评论

最近一直在做博客优化,发现没有关键字和描述,但是又不想用插件实现,觉得插件实现太笨了就想办法

能不能用函数或者其他代码来加载。

后来研究 了相关插件代码,和一些介绍,总算是搞定了,现在分享给大家。

如果你有更好的办法可以告诉我啊。

<?php if(is_single()){

$keywords = ” “;

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

foreach ($tags as $tag ) {

$keywords = $keywords . $tag->name . “,”;

}

if (have_posts()) : while (have_posts()) : the_post();

$text = get_the_excerpt();

if(strlen($text) > 150) {

$text = substr($text , 0, 150);

}

endwhile; endif; ?>

if ($post->post_excerpt){

$text = mb_substr(strip_tags($post->post_excerpt),0,150);

} else {

$text = mb_substr(strip_tags($post->post_content),0,150);

}? >

<meta name=”keyword”  content=”<?php echo $keywords;  ?>”/>

<meta name=”description”  content=”<?php echo $text; ?>”/>
<?php } else { ?>

<meta name=”keyword”  content=”草根评论,时事经济,网络热点,IT视点,最新软件,网站SEO,心情故事”/>

<meta name=”description”  content=”<?php  bloginfo(‘description’); ?>”/>

<?php }  ?>

具体介绍就不说了,把以上代码加到head里面去,当然也可以给没给分类或者是page都加上,这随个人喜好而定。

ps:还要加上编码转换 和空格过滤就完善了.

转载请注明:夜阑小雨 » wordpress如何获取关键字及描述方法

喜欢 (0)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
(1)个小伙伴在吐槽
  1. 先试试
    开心网2010-05-05 16:54 回复