Support

Account

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.