Support

Account

Home Forums ACF PRO Repeater with Post Object not returning expected results.

Solved

Repeater with Post Object not returning expected results.

  • Custom post type > Profiles.
    Collects the name, titles, headshots, etc in ACF fields.

    ACF > Profile
    A simple repeater with a Post Object (selects the Profiles CPT)
    I am using it for certain categories posts (speakers, events, etc)

    I have this in my template, I expect it to return the link and title of the custom post selected in the repeater field. But it is returning the link and title of the current post.

     <?php if ( have_rows( 'profile' ) ) : ?>
          	<?php while ( have_rows( 'profile' ) ) : the_row(); ?>
          		<?php $profile_link = get_sub_field( 'profile_link' ); ?>
          		<?php if ( $profile_link ) : ?>
          			<?php $post = $profile_link; ?>
          			<?php setup_postdata( $post ); ?>
          			<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
          			<?php wp_reset_postdata(); ?>
          		<?php endif; ?>
          	<?php endwhile; ?>
          <?php else : ?>
          	<?php // no rows found ?>
          <?php endif; ?>

    What is odd is that this will pull the correct content.

    <?PHP
    $image = $profile->headshot;
    $creds = $profile->credentials;
    echo $image; 
    echo $creds; 
    ?>

    How do I get the page title and link of a post object in a repeater field?

    Thanks in advance!

  • Got it working with this. Hope it helps someone.

    <?php if ( have_rows( 'profile') ) : ?>
              <div class="container px-5 mx-auto flex flex-wrap">
                <div class="flex flex-wrap -m-4">
    	       <?php while ( have_rows( 'profile' )) : the_row(); ?>
               <?php $profile_link = get_sub_field( 'profile_link' );?>
                <?php if ( $profile_link ) :
                  $permalink = get_permalink( $profile_link->ID );
                  $title = get_the_title( $profile_link->ID );
                  $creds = get_field( 'creds', $profile_link->ID );
                  $headshot_id = get_field( 'headshot', $profile_link->ID ); // Profile headshot media id
                  ?>
                    <div class="row w-48 mr-8" >
                    <?php echo wp_get_attachment_image( $headshot_id, 'thumbnail', '', ["class" => "twn-profile-pic mx-auto"] );?>
                    <p class='text-sm is-all-caps text-center mt-4'><?php echo $title; ?></p>
                      </div>
                      <?php wp_reset_postdata(); ?>
                      <?php endif; ?>
    	       <?php endwhile; ?>
             </div>
             </div>
                <?php else : ?>
    	                <?php // no rows found ?>
              <?php endif; ?>
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.