記事内の一番最初の画像を取得
functions.phpに記載
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php // 画像取得 function catch_that_image($post_id = NULL) { if ( !empty($post_id) ) { $posts = get_post($post_id); $post_content = $posts->post_content; } else { global $post, $posts; $post_content = $post->post_content; } $first_img = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.*?src=(["\'])(.+?)\1.*?>/i', $post_content, $matches); $first_img = $matches[2][0]; if ( empty($first_img) ) { $first_img = ''; } return $first_img; } ?> |