Home › Forums › General Issues › How to insert featured image from related post
Here’s the problem…
I’ve got two post types, let’s say PARTNER and PRODUCT.
In WordPress, each of those Product posts has the relationship field added using ACF, and is connected using a post-object relationship connection to a Partner post.
On my archive index page of Product posts, I’d like each displayed item to either
1) use it’s own featured image if it has one
OR IF NOT
2) use the featured image from it’s connected Partner type post.
Here’s the code I have at the moment to make that happen:
<?php
if(has_post_thumbnail()) { ?>
<?php the_post_thumbnail('small', array('class' => 'thumbnail scale-with-grid')); ?>
<?php } else { ?>
<!-- logo pulled in from the partner/provider page -->
<?php $posts = get_field('product_partner'); if( $posts ): ?><?php foreach( $posts as $post): ?><?php setup_postdata($post); ?>
<?php the_post_thumbnail('small', array('class' => 'thumbnail scale-with-grid')); ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?><?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php } ?>
And that sort of works in that it pulls the correct image, but IF it gets the featured image from the related post (instead of the same post-type post), then it no longer displays the other information below the image that’s part of the same post….title, description, etc.
In other words, The “loop” that brings in the image from the related Partner post doesn’t close, the displayed content doesn’t revert back to the original loop that should be displaying all Product information.
Any ideas?
Using the setup_postdata
function can cause many problems. I would advise that you don’t use this function.
It is always possible to load in data (feature image) from a specific post, by passing in the $post_id parameter ( $post->ID )
Please jump on google, search for the the_post_thumbnail
function, read the docs an find a related function that allows you to send through a post ID.
Thanks
E
Did you ever figure this out? I’m having the same issue. Google Search is not helping.
To get the featured image from relationship, you need to use:
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'medium_large'); ?>
This will grab the featured image URL from the ID, once you have the url, you can use it in img src or as an background.
Hope this helps.
The topic ‘How to insert featured image from related post’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.