Home › Forums › General Issues › Relationship Field on single-cpt.php › Reply To: Relationship Field on single-cpt.php
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>
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.