サポート » 使い方全般 » 記事の最初の画像を複数件表示させたい

  • 記事の最初の画像を表示させることはできましたが現状は1件取得です。
    こちらを最新10件表示させたい場合はどのように記述すればよろしいのでしょうか。
    色々試してみたのですがうまくいかず困っています。
    おわかりの方がいらっしゃったらお願いいたします。

    function.php
    -------------------------
    function catch_that_image() {
        global $post, $posts;
        $first_img = '';
        $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/default.jpg";
        }
        return $first_img;
    }
    表示させるphp内
    <?php
    if ( $first_image = catch_that_image() ) : ?>
    <div class="box">
    	<img src="<?php echo $first_image; ?>" width="300" alt="<?php the_title(); ?>" />
    </div>
    <?php endif; ?>

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック投稿者 wtakop

    (@wtakop)

    英語のサイトで同トピックのものを見つけました。
    https://wordpress.org/support/topic/retreive-first-image-from-post
    すみません。ループのことを忘れていました。
    これで表示できましたが間違っていないでしょうか。不勉強ですみません。

    表示させるphp内
    
    <?php query_posts('showposts=10'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php
    if ( $first_image = catch_that_image() ) : ?>
    	<img src="<?php echo $first_image; ?>" width="300" alt="<?php the_title(); ?>" />
    <?php endif; ?>
      <?php endwhile;?>

1件の返信を表示中 - 1 - 1件目 (全1件中)
  • トピック「記事の最初の画像を複数件表示させたい」には新たに返信することはできません。