This works. Now just need to eliminate the current product from the list.
$relatedImages = get_field('images', $product->ID);
$size = 'related-product-thumbnail';
$relatedThumb = $relatedImages['sizes'][ $size ];
?>
<li>
<a href="<?php echo get_permalink( $product->ID ); ?>" title="<?php echo get_the_title( $product->ID ); ?>">
<span><img src="<?php echo $relatedThumb ?>"></span>
<span class="related-product-title"><?php echo get_the_title( $product->ID ); ?></span></a>
</li>
Thanks James. That almost works. The loop through the relationship field is looping the correct number of times, but it is returning the same product every time (the same product loaded by the single-scpl_product.php template). I actually want to remove that product from the loop.
<div class=related-products>
<h3>Products In This Collection</h3>
<?php
$collections = get_posts(array(
'post_type' => 'scpl_collection',
'meta_query' => array(
array(
'key' => 'products',
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
)); ?>
<?php foreach( $collections as $collection ):
$products = get_field('products', $collection->ID); ?>
<ul>
<?php foreach( $products as $product ):
$relatedImages = get_field('images');
$size = 'related-product-thumbnail';
$relatedThumb = $relatedImages['sizes'][ $size ];
?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<span><img src="<?php echo $relatedThumb ?>"></span>
<span class="related-product-title"><?php the_title(); ?></span></a>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
I got there by storing Post A’s field’s in variables, then in the same loop started the foreach loop as prescribed by ACF, also storing the results from Post B in a variable. Then just echo’d it all out in an ul.
If there is a better way I would still be interested in hearing it. Thanks.
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.