WordPress 获取文章中第一张图片并显示图片方法代码

wordpress 实现这个首页自动显示文章内容缩略图的功能。

//获取文章第一张图片,如果没有图就会显示默认的图  function catch_the_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)){           $first_img = bloginfo('template_url'). '/images/default-thumb.jpg';      }      return $first_img;   }

使用时,直接在循环里使用这个方法就可以了:

 catch_the_image();

腾讯云限时秒杀【点击购买】

搬瓦工,CN2高速线路,1GB带宽,电信联通优化KVM,延迟低,速度快,建站稳定,搬瓦工BandwagonHost VPS优惠码BWH26FXH3HIQ,支持<支付宝> 【点击购买】!

Vultr$3.5日本节点,512M内存/500G流量/1G带宽,电信联通优化,延迟低,速度快【点击购买】!

阿里云香港、新加坡VPS/1核/1G/25G SSD/1T流量/30M带宽/年付¥288【点击购买】