免插件为文章自动为文章添加关键词【WP系列教程之五】

免插件自动为文章添加关键词

我们在用wordpress写文章的时候不免要为每一篇文章设置相关的关键词,甚是麻烦,今天贴出一段函数代码,可以达到免插件为文章自动为文章添加关键词的目的,将以下代码加入主题目录下的function.php文件中即可实现,话不多说上代码:

//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);

首页描述
//seo 代码
function bigfa_description() {
global $s, $post;
$description = '';
$blog_name = get_bloginfo('name');
if ( is_singular() ) {
$ID = $post-&gt;ID;
$title = $post-&gt;post_title;
$author = $post-&gt;post_author;
$user_info = get_userdata($author);
$post_author = $user_info-&gt;display_name;
if (!get_post_meta($ID, "meta-description", true)) {$description = $title.' - 作者: '.$post_author.',首发于'.$blog_name;}
else {$description = get_post_meta($ID, "meta-description", true);}
} elseif ( is_home () ) { $description = "我是博客首页的描述,么么哒"; // 首页要自己加,如果你主题有后台设置可以连接上
} elseif ( is_tag() ) { $description = single_tag_title('', false) . " - ". trim(strip_tags(tag_description()));
} elseif ( is_category() ) { $description = single_cat_title('', false) . " - ". trim(strip_tags(category_description()));
} elseif ( is_archive() ) { $description = $blog_name . "'" . trim( wp_title('', false) ) . "'";
} elseif ( is_search() ) { $description = $blog_name . ": '" . esc_html( $s, 1 ) . "' 的搜索結果";
} else { $description = $blog_name . "'" . trim( wp_title('', false) ) . "'";
}
$description = mb_substr( $description, 0, 220, 'utf-8' );
echo "&lt;meta name="description" content="$description"&gt;n";
}
add_action('wp_head','bigfa_description');

古风网版权互动须知

本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除

原文地址:https://www.vivst.com/598.html 免插件为文章自动为文章添加关键词【WP系列教程之五】

发表评论