HotNews主题集成的随机缩略图本来是为懒人准备的,可能有童鞋感觉,不同的分类内容都显示相同的随机缩略图,有些不搭调,如果博客日志较多,文章中又无图片,重新编辑添加图片工作量又太大,来个折中的办法,改动一下随机缩略图调用函数,让不同的分类显示不同的随机缩略图。
首先,在HotNews主题images目录新建名称为:random1、random2、random3………文件夹,并在其中放置不同的随机缩略图片,图片名称必须是连续的。
其次,打开主题functions.php模版,找到:
- //支持外链缩略图
- if ( function_exists(‘add_theme_support’) )
- add_theme_support(‘post-thumbnails’);
- /*Catch first image (post-thumbnail fallback) */
- function catch_first_image() {
- global $post, $posts;
- $first_img = ”;
- ob_start();
- ob_end_clean();
- $output = preg_match_all(‘/<img.+src=['“]([^'”]+)[‘”].*>/i’, $post->post_content, $matches);</img.+src=['
- $first_img = $matches [1] [0];
- if(emptyempty($first_img)){ //Defines a default image
- $random = mt_rand(1, 20);
- echo get_bloginfo ( ‘stylesheet_directory’ );
- echo ‘/images/random/’.$random.’.jpg’;
- }
- return $first_img;
- }
替换为:
- //支持外链缩略图
- if ( function_exists(‘add_theme_support’) )
- add_theme_support(‘post-thumbnails’);
- /*Catch first image (post-thumbnail fallback) */
- function catch_first_image() {
- global $post, $posts;
- $first_img = ”;
- ob_start();
- ob_end_clean();
- $output = preg_match_all(‘/<img.+src=['“]([^'”]+)[‘”].*>/i’, $post->post_content, $matches);</img.+src=['
- $first_img = $matches [1] [0];
- if(emptyempty($first_img)){ //Defines a default image
- $random = mt_rand(1, 20);
- echo get_bloginfo ( ‘stylesheet_directory’ );
- if ( is_category( ‘472’ ) ) {
- echo ‘/images/random1/’.$random.’.jpg’;
- } elseif ( is_category( ‘473’ ) ) {
- echo ‘/images/random2/’.$random.’.jpg’;
- } elseif ( is_category( ‘474’ ) ) {
- echo ‘/images/random3/’.$random.’.jpg’;
- }
- }
- return $first_img;
- }
其中:
数字20是随机缩略图数量,根据实际自行修改。
修改上面代码类似“ is_category( ‘472’ )”中数字为相应的分类ID号
random3是图片文件夹的名称
如果分类较多,可以多复制几个:
- elseif ( is_category( ‘474’ ) ) {
- echo ‘/images/random3/’.$random.’.jpg’;
- }
同样要修改其中的分类ID及图片文件夹名称。
注:此方法只支持分类页面,首页无效