WordPress文章随机缩略图调用的方法

//支持外链缩略图 Edit by laobuluo.com
if ( function_exists(‘add_theme_support’) )
add_theme_support(‘post-thumbnails’);
function catch_first_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))
{
$image_size = getimagesize($first_img);
$image_width = $image_size[0];
}
//如果第一张图不存在或过小,则返回随机图片
if(empty($first_img) || $image_width<50){ $first_img = ''; //从2张图中随机选择,可根据自己的图片数量设置 $random = mt_rand(1, 10); echo get_bloginfo ( 'stylesheet_directory' ); echo '/images/random/'.$random.'.JPG'; } return $first_img; } 我们将上面代码复制到当前主题的 Functions.php 文件中,检查兼容有没有问题,有问题需要排查。然后我们需要在主题"/images/random/"目录中。新建1-10.jpg 图片放到目录中。 然后我们在需要调用缩略图的文章列表合适位置调用。 <,?php echo catch_first_image(); ?,>

这样可以实现效果。

转载请注明本文链接:https://blog.weixiaoline.com/2165.html