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!
Accordions are a great way to group related information while allowing users to interactively show and hide content. In this video, Damon Cook goes in-depth on how to create an accessible accordion block using ACF PRO’s Repeater field.https://t.co/RXT0g25akN
— Advanced Custom Fields (@wp_acf) March 2, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.