• Resolved slurve

    (@slurve)


    Pretty sure I’m doing something wrong here, so if anyone can point me in the right direction I’d appreciate it. Basically, I’m trying to loop through posts and display a featured image called secondary-image (I’m also using Advanced Custom Fields and YARPP). The code below seems to be ignoring $related_story->ID in the_post_thumbnail(). I can echo the ID, but just can’t pass it to MultiPostThumbnails::the_post_thumbnail(). If I change it to NULL, it works, but displays the secondary-image that’s attached to the current post (this code is in single.php inside the loop). The post titles and permalinks work, but the thumbnails don’t. Any ideas? Sorry if this is really confusing. Perhaps I need to explain it better.

    while ( has_sub_field('choose_stories') ) {
     $related_story = get_sub_field('custom_story');
     ?>
     <a href="<?php echo get_permalink($related_story->ID); ?>">
     <?php if ( MultiPostThumbnails::has_post_thumbnail('post', 'secondary-image', $related_story->ID) ) { ?>
      <?php echo MultiPostThumbnails::the_post_thumbnail('post', 'secondary-image', $related_story->ID); ?>
     <?php } else { ?>
      <img src="<?php bloginfo('template_directory'); ?>/images/m.png" alt="<?php echo $related_story->post_title; ?>">
     <?php } ?>
     </a>
     <?php
    }

    https://www.ads-software.com/plugins/multiple-post-thumbnails/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Chris Scott

    (@chrisscott)

    That looks correct…

    A few things to check (via xdebug or var_dump in the right place):

    • That the post with the ID that is $related_story->ID actually has a post thumbnail with the id ‘secondary-image’ associated with it and it’s post type is ‘post’.
    • That MultiPostThumbnails::the_post_thumbnail() is getting the correct ID passed in when you pass in $related_story->ID
    Thread Starter slurve

    (@slurve)

    Ah, you’re correct. The ID was correct all along, I just didn’t have a ‘secondary-image’ attached to the post. Well, I did, but the actual image file wasn’t in my /uploads directory so it wasn’t loading the fallback m.png image (and instead loading a broken image which Chrome doesn’t display).

    Anyway, I think I’m good now. Thanks for the plugin and the response!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem passing ID to the_post_thumbnail()’ is closed to new replies.