• 解決済 scheppen15

    (@scheppen15)


    こんにちは。

    loop.phpで画像があれば表示 なければ非表示で記事本文のみという処理をさせたいのですが、

    画像が一番古い記事にしか表示できません。

    ソースはこちらです。

    <?php
    if ( have_posts() ) : while ( have_posts() ) : the_post();
    ?>

    <div class=”post”>
    <p>
    <?php
    $imgset = array(
    ‘post_type’ => ‘attachment’,
    ‘post_mime_type’ => ‘image’,
    ‘post_parent’ => $post->ID,
    ‘numberposts’ => ‘1’
    );
    $images = get_children($imgset);
    $image = array_shift($images);
    ?>

    <?php if($image): ?>
    <h2>“><?php the_title(); ?></h2>
    <?php echo wp_get_attachment_image($image->ID, ‘thumbnail’); ?>
    <p><?php echo mb_substr(strip_tags($post-> post_content), 0, 150); ?></p>

    <?php else: ?>
    <h2>“><?php the_title(); ?></h2>

    <p><?php echo mb_substr(strip_tags($post-> post_content), 0, 55); ?></p>

    <?php endif; ?>
    </p>

    </div>

    <?php
    endwhile;
    endif;
    ?>

    どなたか、わかる方がいれば手助けしていただきたいのです。

    厚かましい話ですが、もし時間があればよろしくお願いいたします。

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

    (@scheppen15)

    コードを変更してみましたが、次は画像の表示ができましたが、
    記事、画像ともに1つしか表示されません。

    <?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)){
    $first_img = "/no_image.jpg";
    }
    return $first_img;
    }
    ?>
    
           <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    	<a href="<?php the_permalink(); ?>"><img src="<?php echo catch_that_image() ?>" /></a>
    
    <p><?php echo mb_substr(strip_tags($post-> post_content), 0, 150); ?></p>
    トピック投稿者 scheppen15

    (@scheppen15)

    自己解決いたしました。

2件の返信を表示中 - 1 - 2件目 (全2件中)

トピック「ループ処理をしているけども画像が1つしか表示されない」には新たに返信することはできません。