标题 | hotnews主题不同分类显示不同的随机缩略图 |
范文 | 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); $first_img = $matches [1] [0]; if(empty($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); $first_img = $matches [1] [0]; if(empty($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及图片文件夹名称。 |
随便看 |
|
在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。