Home › Forums › Add-ons › Repeater Field › Repeater Relationship Post Fields from Array › Reply To: Repeater Relationship Post Fields from Array
[Edited to remove sleepy mistakes…]
Elliot, thanks again for the info. For anyone who is interested, I wanted to post some code here that worked for me in using a post_object within a repeater. I had originally started this way but couldn’t find many examples on post_object syntax to copy… That’s why I went with the relationship in a repeater.
Your example for a relationship within a repeater worked for sure, but the interface was actually a little bit difficult for my editor to figure out because it allows multiple entries within one repeater row. The single-select post_object seems better to me because we want just 1. related case A, custom description A … 2. related case B, custom description B.
Here is my first pass at adapting code from this other question about post objects in repeaters. I’m probably doing a number of things wrong here (I run it twice, there are probably unused variables), but I get the result I’m looking for, at 3am no less! If anyone cares to optimize or correct me, please feel free!
THANKS
<!-- CASE STUDIES
================================== -->
<?php
if ( get_field('strategy_case_studies') ): ?>
<section class="valueCaseStudies">
<div class="container">
<div class="row">
<div class="col-md-8">
<h3>INTERNAL Case Studies</h3>
<div class="row">
<?php while( has_sub_field('strategy_case_studies'))
{ ?>
<?php
$the_case->post_title; //post title of related case selected in repeater
$case_type = get_field('case_study_type', $the_case); // internal or external
$the_case = get_sub_field('s_cs_select'); // the repeater
$case_desc = get_sub_field('s_cs_description'); // the custom description
?>
<?php if ( $case_type == 'Internal' ) { ?>
<div class="col-md-6">
<p><strong><?php echo get_the_title($the_case->ID); ?></strong>
<br /><?php echo $case_desc; ?>
<a href="<?php echo get_permalink($the_case->ID); ?>">(link)</a>
</p>
</div>
<?php } else {} ?>
<?php } ?>
</div>
</div>
<?php endif; ?>
<?php if ( get_field('strategy_case_studies') ): ?>
<div class="col-md-4">
<h3>EXTERNAL Case Studies</h3>
<?php while( has_sub_field('strategy_case_studies'))
{ ?>
<?php
$the_case->post_title; //post title
$the_case = get_sub_field('s_cs_select');
$case_type = get_field('case_study_type', $the_case);
$case_desc = get_sub_field('s_cs_description');
?>
<?php if ( $case_type == 'External' ) {
// trying to display internal and external types separately was hard to figure out ... this is probably not the best way
?>
<p><strong><?php echo get_the_title($the_case->ID); ?></strong>
<br /><?php echo $case_desc; ?>
<a href="<?php echo get_permalink($the_case->ID); ?>">(link)</a>
</p>
<?php } else {} ?>
<?php } ?>
</div>
</div>
</div>
</section>
<?php endif; ?>
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.