Home › Forums › Add-ons › Repeater Field › Display repeater child field value
Hello,
We are using Repeater Field as related posts. So, what we do is when we create a post/custom post, We select few other posts as related.
We want to use it as widget with post title and it’s link. We use the following code.
<?php while(has_sub_field('repeater_related_items')): ?>
<?php $url = get_sub_field('related_item'); ?>
<li><?php echo $title = get_the_title(url_to_postid($url)); ?></li>
<?php endwhile;?>
Is there a way to do something like this?
Regard’s
Hi @warpx,
I reckon you will need to use our post object. 🙂
It will return a WP post object that you can access using the standard WP functions. Eg:
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
Checkout out our example here => http://www.advancedcustomfields.com/resources/field-types/post-object/
Hello,
I have the following code
<?php $post_objects = get_field('repeater_related_items');
if( $post_objects ): ?>
<ul>
<?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span><?php the_field('repeater_related_items'); ?></span>
</li>
<?php endforeach;?>
</ul>
<?php wp_reset_postdata(); ?>
<?php endif;?>
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Main Sidebar') ) : endif; ?>
The problem is that when I add this to sidebar other widget doesn’t work.
Hi @warpx
Does your custom code work? Is it causing a PHP error to cut off the rest of the page?
It’s possible that the setup_postdata
function is causing the widgets to misbehave.
Perhaps you can test this by removing the setup_postdata
function and the wp_reset_postdata()
.
Your loop code may not work as expected (due to the incorrect $post object), but it will help debug the issue.
Thanks
E
Hello,
Thanks for your help. I found the issue though, Simply removed
<span><?php the_field('repeater_related_items'); ?></span>
Thanks
The topic ‘Display repeater child field value’ is closed to new replies.
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.