<?php function catch_that_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 $first_img = "画像がない場合の画像の場所"; } return $first_img; } ?>
![記事内の一番最初の画像を自動で表示する『catch_that_image()』でうまく最初の画像が取得できない](https://cdn-ak-scissors.b.st-hatena.com/image/square/0644a82d5842249df75810cbe9535841b4955712/height=288;version=1;width=512/https%3A%2F%2Fmauyas.com%2Fwp-content%2Fuploads%2F2013%2F04%2F20134214675_thumb.jpg)