Home › Forums › Add-ons › Repeater Field › Post object field inside repeater field
Hello
I am aware that there are countless forum entries on this topic, but none of them has really brought me a solution. For sure I have a basic understanding problem and would therefore be very grateful for your help.
It’s about a custom post called ‘translations’, which have a repeater field for the corresponding people who do translations (translation_translator). There it has a field for a flag of the corresponding language, which works wonderfully. Then it has the name, which is a post object, i.e. from another custom post type called ‘translator’, which has e.g. the first name (translator_first_name).
The code so far:
`<div class=”row”>
<?php
if( have_rows(‘translation_translator’) ):
while ( have_rows(‘translation_translator’) ) : the_row();
$language = get_sub_field(‘translator_language’);
?>
<div class=”col-2″>
/img/flags-iso/<?php echo $language->slug; ?>.png” title=”<?php echo $language->name; ?>”> <?php echo $language->name; ?>
</div>
<div class=”col-10″>
<?php $name_object = get_sub_field(‘translation_translator’);
if( $name_object ): ?>
<?php
$name = $name_object;
setup_postdata( $name );
echo the_field(‘translator_first_name’);
endif; ?>
</div>
<?php
endwhile;
endif;
?>
</div>’
The part inside the col-10 should show the translators name from an other custom post named “translator” with the field “translator_name”. I can’t manage to access from one to the other and as I said I lack a basic understanding here somewhere. Thanks for any help!
Adriano
Hi @sonnenschauer,
Please try out the following code as the basis of implementation which I’ve tested on my end:
<?php if ( have_rows( 'translation_translator' ) ) : ?>
<?php while ( have_rows( 'translation_translator' ) ) :
the_row(); ?>
<?php
$translator_language = get_sub_field( 'translator_language' );
if ( $translator_language ) :
$post = $translator_language;
setup_postdata( $post );
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
I hope this helps!
Thanks for your reply! Unfortunately the code doesn’t work yet, no output. I wonder if I have missed something in the configuration of the fields. The return format of the repeater field is “Post object”. Also, I modified your code a bit so that I don’t query the rows twice. The code currently looks like this:
<?php
if( have_rows('translation_translator') ):
while ( have_rows('translation_translator') ) : the_row(); ?>
<?php
$language = get_sub_field('translator_language');
?>
<div class="col-2">
<img src="<?php echo get_stylesheet_directory_uri(); ?>/img/flags-iso/<?php echo $language->slug; ?>.png" title="<?php echo $language->name; ?>"> <?php echo $language->name; ?>
</div>
<div class="col-10">
<?php
$translator_name = get_sub_field( 'translator_name' );
if ( $translator_name ) :
$post = $translator_name;
setup_postdata( $post );
?>
<?php the_sub_field('translator_first_name') ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
</div>
<?php
endwhile;
endif;
?>
Yes, I finally figured out how to show the name! I’ve watched again Mr.Digitals Tutorials on YouTube, simplified your solution, made some trial and errors and finally it works. The code looks like this now:
<?php
if( have_rows('translation_translator') ):
while ( have_rows('translation_translator') ) : the_row(); ?>
<?php
$language = get_sub_field('translator_language');
?>
<div class="col-2">
<img src="<?php echo get_stylesheet_directory_uri(); ?>/img/flags-iso/<?php echo $language->slug; ?>.png" title="<?php echo $language->name; ?>"> <?php echo $language->name; ?>
</div>
<div class="col-5">
<?php
$translator = get_sub_field('translator_name');
echo $translator->translator_first_name." ";
echo $translator->translator_last_name;
?>
</div>
<div class="col-md-5">
<?php $payment_method = get_sub_field('translator_payment_method');
echo $payment_method;
?>
</div>
<?php
endwhile;
endif;
?>
In the end, it was much simpler than expected. Just putting the sub_field in a variable and the access it.
$translator = get_sub_field('translator_name');
echo $translator->translator_first_name." ";
echo $translator->translator_last_name;
But there are still a few challenges open. Let’s see how it works …
You must be logged in to reply to this topic.
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.