0

I’m developing my own top-down template, loading all pages in the main page. My problem is that in the places of images appears only the last to be charged. For example, if I have three pages, each page with a different image, the image that will appear is always the image of the last page.

I do not know what the problem is, I’ve seen problems cache, problems with the same variable names and can not solve it.

You can see how can i do that:

<section id="about">
  <?php
    $about = get_page_by_title( 'About' );
    $thumb_id = get_post_thumbnail_id($about);
    $abouturl = wp_get_attachment_thumb_url($thumb_id);
  ?>
  <span><?php echo $about->post_title ?></span>
  <img src="<?php echo $abouturl ?>"/>
</section>
<section id="news">
  <ul>
    <?php
    $posts = get_posts(['cat' => get_cat_ID('News'), 'limit' => 3, 'order' => 'DESC']);
    foreach($posts as $post){
      setup_postdata($post);
      $thumb_id = get_post_thumbnail_id($post, 'news-image');
      $url = wp_get_attachment_thumb_url($thumb_id, '300, 300');
    ?>
    <li data-link="<?php the_permalink() ?>">
      <img src="<?php echo the_post_thumbnail_url(array(300, 150)); ?>"/>
      <span>
        <?php echo the_title(); ?>
      </span>
    </li>
    <?php
    wp_reset_postdata();
     }?>
  </ul>
</section>
<section id="contacts">
 <?php
    $contacts = get_page_by_title( 'Contacts' );
    $thumb_id = get_post_thumbnail_id($contacts);
    $contactsurl = wp_get_attachment_thumb_url($thumb_id);
  ?>
  <span><?php echo $contacts->post_title ?></span>
  <img src="<?php echo $contactsurl ?>"/>
</section>

How can i solve that? If you need any more information please ask.

Thank’s

0

Im guessing it is because you are using

echo the_post_thumbnail_url(array(300, 150));

instead of

the_post_thumbnail_url(array(300, 150));

the_post_thumbnail_url echos the output for you.

Another observation, the the_post_thumbnail_url uses the post data that is setup using setup_postdata, so there is no need for these two lines:

$thumb_id = get_post_thumbnail_id($post, 'news-image');
$url = wp_get_attachment_thumb_url($thumb_id, '300, 300');
2
  • doesn't work yet! And if I take the last url others are also blank. On page load to see the all images loaded but then just assume the last. – user3242861 Oct 13 '16 at 12:11
  • Do you mind updating your code example with all code. – stims Oct 13 '16 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.