整理关键词及og标签,wordpress自动添加关键词

之前有人说过我的页头meta有些多,于是抽空整理了一下,整理完发现并没有减少。只是添加了几个判断,更规矩了一些。

精简

效果如图,关键词及描述可自动获取,可定义,根据分类,页面,搜索页面,标签等等都可以自定义或者默认获取。

代码如下,直接添加到模版函数functions.php文件即可。

function wp_sheruoseo(){
  global $s, $post;
    $description = '';
    $keywords = '';
if (is_home()) {// 将以下引号中的内容改成你的主页description
   $description = "设若、一个专注知识产权与经验分享的博客。SheRuo.com";
   // 将以下引号中的内容改成你的主页keywords
   $keywords = "sheruo,设若,设若博客,知识产权博客,商标博客,专利博客";
}
elseif (is_single()) {//文章页关键词及描述
   $description1 = get_post_meta($post->ID, "description", true);
   $description2 = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8'));
   // 填写自定义字段description时显示自定义字段的内容,否则使用文章内容前200字作为描述
   $description = $description1 ? $description1 : $description2;
   // 填写自定义字段keywords时显示自定义字段的内容,否则使用文章tags作为关键词
   $keywords = get_post_meta($post->ID,"keywords",true);
   if($keywords == '') {
      $tags = wp_get_post_tags($post->ID);    
      foreach ($tags as $tag ) {        
         $keywords = $keywords . $tag->name .",";    
      }
	  foreach ( get_the_category ( $post->ID ) as $category )
            $keywords .= $category->cat_name . ',';
      $keywords = rtrim($keywords, ', ');	   
     }
}
elseif (is_category()) {//定义分类添加描述和关键词
     if (is_category('生活')) {//输入分类名称,自定义分类关键词及描述
     $description = category_description();//此处描述可自定义,输入格式:'这里填写描述';
     $keywords = '生活,设若,生活博客';
     }
    elseif (is_category('笔记')) {//输入分类名称,自定义分类关键词及描述,分类多复制本段定义即可。
     $description = category_description();
     $keywords = '笔记,设若,折腾记录,储备库';
     }
    elseif (is_category('知识产权')) {//输入分类名称,自定义分类关键词及描述,分类多复制本段定义即可。
     $description = category_description();
     $keywords = '知识产权,商标,专利,版权,设若';
     }
     else { // 分类的默认描述及关键词,可以到后台 - 文章 -分类目录,修改分类的描述,关键词默认显示名称
     $description = category_description();
     $keywords = single_cat_title('', false);
	 }
}
elseif (is_page()){//定义页面添加关键词和描述
   if (is_page('关于')){
    $description =  str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8'));
    $keywords = '关于设若,设若网,sheruo';
    }
   elseif (is_page('问答')) {
     $description = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8'));
     $keywords = '商标问答,专利问答,侵权问答,法规';
	}
	elseif (is_page('归档')) {
     $description = '所有文章都在这里,设若、一个专注知识产权与经验分享的博客。SheRuo.com';
     $keywords = '文章归档,设若归档,设若';
	}
	elseif (is_page('友链')) {
     $description = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8'));
     $keywords = '设若友链,互相连接,友情链接';
	}
	else { // 默认页面的关键词描述
     $description = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "…", 'utf-8'));
     $keywords =  get_the_title('', false); 
    }
}
elseif (is_tag()){// 标签的description可以到后台 - 文章 - 标签,修改标签的描述
   $description = tag_description();
   $keywords = single_tag_title('', false);
}
elseif (is_search()){// 搜索页面的关键词描述
   $description = " '" . esc_html ( $s, 1 ) . "' 的搜索結果";
   $keywords =  esc_html ( $s, 1 );
}
else {//默认页关键词设置
        $description = $blog_name . "有关 '" . trim ( wp_title ( '', false ) ) . "' 的文章";
        $keywords = trim ( wp_title ( '', false ) );
		}
$description = trim(strip_tags($description));
$keywords = trim(strip_tags($keywords));
echo '<meta name="keywords" content="'.$keywords.'" />';
echo '<meta name="description" content="'.$description.'" />';
echo '<meta property="og:type" content="article"/>
	  <meta property="og:locale" content="zh-CN">
	  <meta property="og:site_name" content="'. get_bloginfo('name').'" />		
	  <meta property="og:url" content="'.get_bloginfo( 'url' ).'" />
	  <meta property="article:site_name" content="'. get_bloginfo('name'). '" />
	';
 if(is_single()||is_page()){//判断是文章或者页面输出og标签
echo '<meta property="og:author" content="' .get_the_author_meta( 'display_name', $post->post_author ). '" />
	  <meta property="og:url" content="'.get_permalink().'" />
	  <meta property="og:title" content="'.get_the_title().'" />
	  <meta property="og:description" content="'.$description.'" />
	  <meta property="og:release_date" content="'.get_the_time('c').'"/>
	  <meta property="og:image" content="'.lxtx_post_images_nums($post->ID,1).'" />
      <meta property="article:author" content="'. get_the_author_meta( 'display_name', $post->post_author ). '" />
	  <meta property="article:published_first" content="'.get_the_title().','.get_permalink().'" />
	  <meta property="article:description" content="'.$description.'" />
	  <meta property="article:published_time" content="'.get_the_time('c').'"/>
	  <meta itemprop="image" content="'.lxtx_post_images_nums($post->ID,1).'" />
      <script type="application/ld+json">{
    "@context": "https://ziyuan.baidu.com/contexts/cambrian.jsonld",
    "@id": "'.get_the_permalink().'",
    "appid": "1620534896519901",
    "title": "'.get_the_title().'",
    "images": ["'.fanly_post_imgs().'"],
    "description": "'.fanly_excerpt().'",
    "pubDate": "'.get_the_time('Y-m-d\TH:i:s').'"
}</script>
';}
}
add_action ( 'wp_head', 'wp_sheruoseo' ); 

这里用lxtx_post_images_nums($post->ID,1)获取第一张图片,不用的可以删掉整条meta,或者可以加入下列代码,实现图片获取。

/**
* WordPress获取文章内图片的数量及文章中第一张图片的地址 - 龙笑天下
* https://www.ilxtx.com/wordpress-post-images-numbers.html
*/
function lxtx_post_images_nums($postid=0,$which=0){
    $content = get_post($postid)->post_content;  
    preg_match_all('/<img.*?(?: |\\t|\\r|\\n)?src=[\'"]?(.+?)[\'"]?(?:(?: |\\t|\\r|\\n)+.*?)?>/sim', $content, $strResult, PREG_PATTERN_ORDER);
    if( $which==0 ){
        $output = ( $strResult && isset($strResult[1]) )?count($strResult[1]):0;
    }else if( $which==1 ){
        // 没图时返回默认图片
        $img_url = '/images/default.png';
        // 没图时返回随机图片,则取消下面2行注释,并在主题根目录images中创建random文件夹,再在random文件夹中放置10张jpg格式的图片且命名为1、2、...、10
        $random = mt_rand(1, 3);
        $img_url = 'https://sheruo.com/images/'.$random.'.png';
        $output = ( $strResult && !empty($strResult[1]) && !empty($strResult[1][0]) ) ? $strResult[1][0] : $img_url;
    }
    return $output;  
}
//WordPress获取第一张图片地址结束.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注