Support

Account

Home Forums General Issues Images on page are the same as in the posts loop

Helping

Images on page are the same as in the posts loop

  • I have a custom field ‘banner’ on every page and post. I want to display loop of posts on a page. On top of this page there should be image ‘banner’, and in the loop of posts I’d like to show the ‘banner’ iamge for each post in thumbnail size.
    Sounds easy but whatever I do, the thumbnails are displaying the main page banner instead of their own ‘banner’ images.

    I tried:
    $banner = get_field(‘banner’, $post->ID);
    with no effect

    This is loop I tried

    <?php
    $args = array(
    ‘posts_per_page’=>15
    );

    $top_banner = get_field(‘top_banner’);
    $top_banner_size = $top_banner[‘sizes’][‘medium’];

    $wp_query = new WP_Query($args);
    ?>

      <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
      <li <?php post_class() ?> id=”post-<?php the_ID(); ?>”>
      ” alt=””/>

      <?php endwhile; ?><!– end of the loop –>

    <?php wp_reset_query(); ?>

    and in header I display the main banner image on top of the page using the_field.

    Why all the images are using the main page image? (they sue the same custom field name)

  • Hi @fanta00

    Please add your code using the code tag so it shows properly. Regarding your issue, I believe you can do something like this:

    $wp_query = new WP_Query($args);
    ?>
    
    <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
        <?php $top_post_banner = get_field('top_banner', get_the_ID()); ?>
        <img src="<?php echo $top_post_banner['sizes']['medium']; ?>" alt="<?php echo $top_post_banner['alt']; ?>" />
    <?php endwhile; ?><!– end of the loop –>
    
    <?php wp_reset_query(); ?>

    I hope this helps.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.