Quantcast
Channel: 凌晨四点的蓝 » wordpress主题
Viewing all articles
Browse latest Browse all 9

换主题了,顺便折腾了一下缩略图功能

$
0
0

近期博客更新的频率不如从前了,大概是有些疲惫了,不过更新频率低跟放弃没什么关系,三四年下来了,写博早就成习惯了,不过前两天倒是又换了款主题,WPYOU系列的主题大气一点,很适合中文显示,我用的版本是免费版的,不过免费版的也要在论坛里攒够积分才能下载,所幸花了八块钱充了积分,直接买下来了。

WPINK-Blue主题是带有首页缩略图功能的,不过当博文没有图片时,会显示一张默认图片,有时候连续几篇没图片,清一色的默认图片,看着比较别扭,所以就想着日志没有图片时就使用分类的缩略图,到网上搜了一下如何添加分类缩略图,也不难,不过代码我不是很理解,因为里面有个循环,看的不是很明白,结果是正确的。找到主题的functions.php文件,修改一下catch_that_image函数就可以了:

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('//i', $post->post_content, $matches);
  $first_img = $matches [1] [0];
  if(empty($first_img)){ //如果博文没有图片的话,显示分类缩略图
  	$site_url = bloginfo('template_url');
    //$first_img = "$site_url/images/no-thumb.jpg";
    foreach(get_the_category() as $catList)
    {
    	//上传几张跟分类别名同名的jpg图片到主题目录的images目录下就可以显示了
    	$first_img = $site_url.'/images/'.$catList->category_nicename.'.jpg';
    }
  }
  return $first_img;
}

显示完分类缩略图后,我发现还是不妥,因为有时候连续几篇文章都是一个分类的,这样就是好多重复图片,于是乎,又想着为一个分类多加几个缩略图,随即显示,进一步改一下代码就可以了,我为每个分类设置四张图片,随机显示,这样一般就不会用多张重复的现象了:

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('//i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //如果博文没有图片的话,显示分类缩略图
  	$site_url = bloginfo('template_url');
    //$first_img = "$site_url/images/no-thumb.jpg";
    foreach(get_the_category() as $catList)
    {
    	//上传几张跟分类别名同名的jpg图片到主题目录的images目录下就可以显示了
    	$first_img = $site_url.'/images/'.$catList->category_nicename.rand(1,4).'.jpg';
    }
  }
  return $first_img;
}

Viewing all articles
Browse latest Browse all 9

Trending Articles