为WordPress博客文章添加文章SEO代码
前言
搜索引擎不仅仅会浏览我们的网站首页,还会浏览文章页的,给文章页自动添加一些关键词,文章描述可能会增加SEO亲和度,增加文章收录的机会,使用代码简简单单就好,不需要插件去劳烦。
代码
//SEO //自动关键词与描述 function get_cats_name() { $allcats=get_categories(); foreach ($allcats as $category) { $keywords[] = $category->cat_name; } return $keywords; } // utf8 substr function utf8Substr($str, $from, $len) { return preg_replace('#^(?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$from.'}'. '((?:[x00-x7F]|[xC0-xFF][x80-xBF]+){0,'.$len.'}).*#s', '$1',$str); } // Meta SEO function meta_SEO() { global $post; $output = ''; if (is_single()){//如果是文章页 $keywords = ''; $description = ''; if ($post->post_excerpt) {//如果文章摘要存在就以文章摘要为描述 $description = $post->post_excerpt; $description = str_replace("rn","",$description); $description = str_replace("n","",$description); $description = str_replace(""","'",$description); $description .= '...'; } else {//如果文章摘要不存在就截断文章前200字为描述 $description = utf8Substr(strip_tags($post->post_content),0,200); $description = str_replace("rn","",$description); $description = str_replace("n","",$description); $description = str_replace(""","'",$description); $description .= '...'; } $tags = wp_get_post_tags($post->ID);//取文章标签 foreach ($tags as $tag ) { $keywordarray[] = $tag->name; } //以文章标签为关键字 $keywords = implode(',',array_unique((array)$keywordarray)); } else {//如果不是文章页 $keywords = '乐趣公园,贴吧动态,wordpress,系统操作,网络应用,软件,IT资讯,贴吧,火狐'; //在引号间写入你博客的关键字用,断开 $description = '关注贴吧动态,折腾火狐浏览器';//在引号间写入你博客的简单描述,不要过200字 } //输出关键字 $output .= '<meta name="keywords" content="' . $keywords . '" />' . "n"; $output .= '<meta name="description" content="' . $description . '" />' . "n"; //输出描述 echo "$outputn"; } add_action('wp_head', 'meta_SEO');//添加meta_SEO函数到头部信息里 add_filter( 'comment_text' , 'ludou_comment_add_at', 20, 2);
本段代码网络上找到的,作者不明,本人只是分享
使用
将本段代码插入主题函数文件functions.php即可。
后语
使用插件毕竟会一定程度上降低网站的性能,所以,除非必要,直接代码即可。
-- 完 --
//添加meta_SEO函数到头部信息里。这一段还需要把地址改掉吧?
@iluvut.com 注释而已
你代码中 有add_filter( 'comment_text' , 'ludou_comment_add_at', 20, 2);
@WP 取消之后呢?
还不错!
@陌小雨 其实很多主题都集成了