Using post object inside ACF repeater field

I’m using Advanced Custom Fields on my website.

I have a repeater field called anime_par, with sub_field called animateur. Sub field animateur is a post-object.

Read More

I’m using this inside a loop in my page, a loop that displays posts from a category inside a custom post type.

What I’m trying to do is to display the post name and post link of the animateur selection inside my page.

Here is the code I’m using but it’s not working, it displays the permalink of my current page, not the one selected in the custom field.

<?php while(has_sub_field('anime_par')): ?>

<a href="<?php echo get_permalink('the_sub_field("animateur")'); ?>"><?php echo get_title('the_sub_field("animateur")'); ?></a>

<?php endwhile; ?>

Any suggestions to make this work?

thanks for your help,

Related posts

Leave a Reply

3 comments

  1. This method is working for me, per the repeater and post object docs on ACF. You’ve got to set up the post object inside of the repeater loop.

    I added in your field names, and some completely optional html to show the structure.

    Hope it helps.

    <!-- Start Repeater -->
    <?php if( have_rows('anime_par')): // check for repeater fields ?>
    
    <div class="a-container">
    
        <?php while ( have_rows('anime_par')) : the_row(); // loop through the repeater fields ?>
    
        <?php // set up post object
            $post_object = get_sub_field('animateur');
            if( $post_object ) :
            $post = $post_object;
            setup_postdata($post);
            ?>
    
        <article class="your-post"> 
    
            <?php the_title(); ?>
            <?php the_post_thumbnail(); ?>
            <?php // whatever post stuff you want goes here ?>
    
        </article>
    
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    
        <?php endif; ?> 
    
        <?php endwhile; ?>
    
    </div>
    <!-- End Repeater -->
    <?php endif; ?>
    
  2. the_sub_field doesn’t work without has_sub_field
    What you have to do it is use loop with has_sub_field as it said in the documenration http://www.advancedcustomfields.com/resources/functions/the_sub_field/

    or you can use get_field('repeater_sluf') like that

    $rows = get_field('repeater_field_name' ); // get all the rows
    $first_row = $rows[0]; // get the first row
    $first_row_image = $first_row['sub_field_name' ]; // get the sub field value 
    
  3. <?php if(get_field('favourite_design_quarters', 'user_'.$current_user->ID)): ?>
    
    
    <?php while(has_sub_field('favourite_design_quarters', 'user_'.$current_user->ID)): 
    $company = get_sub_field('company_name');
    $image = wp_get_attachment_image_src( get_post_thumbnail_id( $company->ID ), 'package-thumbnail' ); 
    ?>
                    <tr>
                        <td><img src="<?php echo $image[0]; ?>" alt="<?=$company->post_title;?>" /></td>
                        <td><?=$company->ID;?></td>
                        <td style="text-align:left;"><?=$company->post_content;?></td>
                        <td><?=$company->post_date;?></td>
                        <td><a href="#">Delete</a></td>
                    </tr>