温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

WordPress文章如何自动添加关键词

发布时间:2021-09-02 09:42:36 来源:亿速云 阅读:129 作者:小新 栏目:开发技术

这篇文章将为大家详细讲解有关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文章如何自动添加关键词”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI