Home › Forums › Front-end Issues › Relating posts with Woocommerce products › Reply To: Relating posts with Woocommerce products
So this is assuming you’re using the Relationship
field type with the return format set to Post Object
:
<ul class="medium-block-grid-5 blog-posts">
<?php if ( $products = get_field('related_products') ): ?>
<?php foreach( $products as $p ): ?>
<li class="post">
<a href="<?php the_permalink( $p->ID ); ?>" title="<?php the_title_attribute( array( 'post' => $p->ID ) ); ?>" ><span class="featured-title"><h3><?php echo get_the_title( $p->ID ); ?></h3></span></a>
<?php if ( has_post_thumbnail( $p->ID ) ) : ?>
<?php echo get_post_thumbnail( $p->ID, 'regular-posts' ); ?>
<?php else : ?>
<img src="<?php echo get_template_directory_uri(); ?>/img/no-image.png" alt="" />
<?php endif; ?>
</li>
<?php endforeach; ?>
<?php endif; ?>
</ul>
Summary is that you don’t need to put this inside a WordPress loop, you can just iterate over the related posts and call the desired functions on that specific post (in this cast product) ID.
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.