サポート » 使い方全般 » 任意のサイズで記事の一番最初の画像を取得してサムネイルにしたい

  • 下記方法で、記事内の一番最初の画像を取得してサムネイルにできました。
    この場合、記事内の画像がそのまま表示されますが、
    add_image_size('img500', 500, 500, true);
    のように生成した任意のサイズを表示することは可能でしょうか?

    いろいろと方法を試しましたがなかなかうまくいかず、
    おわかりの方がいらっしゃいましたらお教えいただければ幸いです。

    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 = "/images/noimage.jpg";
        }
        return $first_img;
    }

    <img src="<?php echo catch_that_image(); ?>" />

    • このトピックはurustim3699が7年、 8ヶ月前に変更しました。
  • トピック「任意のサイズで記事の一番最初の画像を取得してサムネイルにしたい」には新たに返信することはできません。