WordPress|記事内の一枚目の画像を取得する
1. function.phpに下記の記述を追加
function catch_that_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)){ //Defines a default image
$first_img = "画像がない場合に表示させる画像のURLを記述する";
}
return $first_img;
}
?>
2. 画像を追加した場所に下記を挿入する
<img src="<?php echo catch_that_image(); ?>" alt="" width="20" height="20" />
これでOK!!
関連記事