网站的要害字及网页形容关系网站对搜寻引擎的敌对水平,假如本人手动加显然太折腾了,那如何让wordpress博客主动为每篇文章主动要害字及网页形容。每篇文章的内容不同,咱们该如何让wordpress主动增加文章形容和要害词呢?上面就让咱们来看看如何给wordpress主动增加文章形容和要害词。
在你主题的functions.php文件增加以下代码,各个代码的性能解析如下:
add_action ( 'wp_head', 'wp_keywords' ); // 增加要害字
add_action ( 'wp_head', 'wp_description' ); // 增加页面形容
function wp_keywords() {
global $s, $post;
$keywords = '';
if (is_single ()) { //假如是文章页,要害词则是:标签+分类ID
if (get_the_tags ( $post->ID )) {
foreach ( get_the_tags ( $post->ID ) as $tag )
$keywords .= $tag->name . ', ';
}
foreach ( get_the_category ( $post->ID ) as $category )
$keywords .= $category->cat_name . ', ';
$keywords = substr_replace ( $keywords, '', - 2 );
} elseif (is_home ()) {
$keywords = '我是主页要害词'; //主页要害词设置
} elseif (is_tag ()) { //标签页要害词设置
$keywords = single_tag_title ( '', false );
} elseif (is_category ()) {//分类页要害词设置
$keywords = single_cat_title ( '', false );
} elseif (is_search ()) {//搜寻页要害词设置
$keywords = esc_html ( $s, 1 );
} else {//默许页要害词设置
$keywords = trim ( wp_title ( '', false ) );
}
if ($keywords) { //输入要害词
echo "<meta name=\"keywords\" content=\"$keywords\" />\n";
}
}
function wp_description() {
global $s, $post;
$description = '';
$blog_name = get_bloginfo ( 'name' );
if (is_singular ()) { //文章页假如存在形容字段,则显示形容,否则截取文章内容
if (! empty ( $post->post_excerpt )) {
$text = $post->post_excerpt;
} else {
$text = $post->post_content;
}
$description = trim ( str_replace ( array (
"\r\n",
"\r",
"\n",
" ",
" "
), " ", str_replace ( "\"", "'", strip_tags ( $text ) ) ) );
if (! ($description))
$description = $blog_name . "-" . trim ( wp_title ( '', false ) );
} elseif (is_home ()) {//首页显示形容设置
$description = $blog_name . "-" . get_bloginfo ( 'description' ) .'首页要显示的形容'; // 首頁要本人加
} elseif (is_tag ()) {//标签页显示形容设置
$description = $blog_name . "无关 '" . single_tag_title ( '', false ) . "' 的文章";
} elseif (is_category ()) {//分类页显示形容设置
$description = $blog_name . "无关 '" . single_cat_title ( '', false ) . "' 的文章";
} 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 "<meta name=\"description\" content=\"$description\" />\n";
}
突出要害字在搜索后果:
function wps_highlight_results($text){
if(is_search()){
$sr = get_query_var('s');
$keys = explode(" ",$sr);
$text = preg_replace('/('.implode('|', $keys) .')/iu', '<strong>'.$sr.'</strong>', $text);
}
return $text;
}
add_filter('the_excerpt', 'wps_highlight_results');
add_filter('the_title', 'wps_highlight_results');
应用此代码段突出显示搜寻词在你的博客搜寻后果the_excerpt和the_title。
以上就是安达网络工作室关于《WordPress的文章自动添加关键词及关键词的SEO优化》的一些看法。更多内容请查看本栏目更多内容!
本文实例讲述了在WordPress治理页面底部自定义文字的办法,分享给大家供大家参考。详细完成办法如下: 把上面...
WordPress 3.5 正式公布, 这个版本包括大量更新. 启用了最新的呼应式规划默许主题 Twenty Twelve; 改版了 A...
more标签 这种办法应该是最灵敏的一种办法,操作也很简略,只要要你在编辑文章的时分拔出more标签 或许应用...
其实我集体并不是很喜爱这种摘要的显示形式,然而这个办法用起来比拟不便而已。 WordPress是有摘要性能...
应用wordpass博客顺序的冤家都晓得他的默许首页是index.php,但是地球人都晓得index.html的太首页可以进步网...
WP的性能弱小在于有支持并完满兼容的插件。自身WP链接治理不好自在排序,CoCo Link Sort 这款插件补偿了这样...