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
<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!
🚀 This week’s session of ACF Chat Fridays dips into the preliminary results of our first ever user survey. Don’t miss it! https://t.co/3UtvQbDwNm pic.twitter.com/kMwhaJTkZc— Advanced Custom Fields (@wp_acf) May 9, 2023
© 2023 Advanced Custom Fields.