修整wordpress主题,添加og属性,优化主题SEO。

从使用 Twenty Twelve 主题以来,一直比较看重 Twenty Twelve主题的简洁,无图,但针对seo上面Twenty Twelve主题只有一行title,想要搜索引擎收录这怎么能行,于是开始针对页眉header.php 文件进行优化,源码效果如下(压缩后有些乱,将就着看吧)

修整wordpress主题,添加og属性,优化主题SEO。

参照之前的文章兜兜转转,最终决定,还是用Twenty Twelve 主题 

主题header.php 文件 title 后加入如下代码

//判断是否是首页,添加keywords和description
<?php
$description = '';
$keywords = '';
if (is_home() || is_page()) {
   // 将以下引号中的内容改成你的主页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 . ", ";    
      }
      $keywords = rtrim($keywords, ', ');
   }
}
elseif (is_category()) {
   // 分类的description可以到后台 - 文章 -分类目录,修改分类的描述
   $description = category_description();
   $keywords = single_cat_title('', false);
}
elseif (is_tag()){
   // 标签的description可以到后台 - 文章 - 标签,修改标签的描述
   $description = tag_description();
   $keywords = single_tag_title('', false);
}
$description = trim(strip_tags($description));
$keywords = trim(strip_tags($keywords));
?>
//keywords和description输出。
<meta name="keywords" content="<?php echo $keywords; ?>" />
<meta name="description" content="<?php echo $description; ?>" />
//添加OG属性,
<meta property="og:type" content="article"/>
//<?php the_time('c'); ?>  时间输出
<meta property="article:published_time" content="<?php the_time('c'); ?>"/> 
<meta property="og:release_date" content="<?php the_time('c'); ?>"/>
//作者输出
<meta property="article:author" content="<?php bloginfo( 'name' ); ?>" />
<meta property="og:author" content="<?php bloginfo( 'name' ); ?>" />
//og属性,连接输出。
<meta property="og:url" content="<?php global $wp; $current_url = home_url(add_query_arg(array(),$wp->request)); echo $current_url; ?>" />
<meta property="og:title" content="<?php wp_title( '|', true, 'right' ); ?>" />
<meta property="article:published_first" content="<?php bloginfo( 'name' ); ?>,<?php global $wp; $current_url = home_url(add_query_arg(array(),$wp->request)); echo $current_url; ?>" />
<meta property="og:site_name" content="<?php bloginfo( 'name' ); ?>" />
<meta property="og:description" content="<?php echo $description; ?>" />
//<?php echo lxtx_post_images_nums($post->ID,1); ?>第一张图片输出,
<meta property="og:image" content="<?php echo lxtx_post_images_nums($post->ID,1); ?>" />
<meta itemprop="image" content="<?php echo lxtx_post_images_nums($post->ID,1); ?>" />

这里重点是第一张图片输出调试了几次,由于关闭了 thumbnail ,所以要在模版函数functions.php添加个图片路径调取的代码,添加代码如下:

/**
* 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 = get_stylesheet_directory_uri().'/images/default.jpg';
        // 没图时返回随机图片,则取消下面2行注释,并在主题根目录images中创建random文件夹,再在random文件夹中放置10张jpg格式的图片且命名为1、2、...、10
        $random = mt_rand(1, 10);
        $img_url = get_stylesheet_directory_uri().'/images/random/'.$random.'.jpg';
        $output = ( $strResult && !empty($strResult[1]) && !empty($strResult[1][0]) ) ? $strResult[1][0] : $img_url;
    }
    return $output;  
}

发表回复

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