Support

Account

Home Forums General Issues WP Shortcode from ACF repeater field?

Solved

WP Shortcode from ACF repeater field?

  • Hi all,

    I’m trying to create a WP shortcode using a repeater field query. This is what I’ve been trying:

    function get_team_members() { 
    
    if ( have_rows( 'team_members' ) ) { 
    	while ( have_rows( 'team_members' ) ) : the_row(); 
    		$post_object = get_sub_field( 'team_member' );
    		if ( $post_object ) {
    			$post = $post_object;
    			setup_postdata( $post ); 
                $headshot = get_field( 'headshot' );
    				$output = '<div class="team-member">';
                        $output .= '<a href="' . the_permalink() . '">';
                            if ( $headshot ) {
                                $output .= '<img src="' . $headshot['url'] . '" alt="' . $headshot['alt'] . '" />';
                            }
                        $output .= '</a>';
                        $output .= '<div class="team-member-info">';
                            $output .= '<div class="team-member-name">' . get_field( 'name' ) . '</div>';
                            $output .= '<div class="team-member-title">' . get_field( 'title' ) . '</div>';
                        $output .= '</div>';
                    $output .= '</div>';
    			wp_reset_postdata();
    		}
    	endwhile;
    }
    
    // return data
    return $output;
    
    }
    add_shortcode( 'show-team-members', 'get_team_members' );

    However, on the page I only see the empty div tags, and there’s buffer output at the top of the page with partial URLs. It should say “https://mydomain.com/team_members/name&#8221; but it’s only getting as far as “https://mydomain.com/team&#8221; and then repeating for as many rows are present. Where am I going wrong? And thanks for the help!

  • You are attempting to use the global $post inside of a function without defining it. Add this to the top of your function

    
    global $post;
    
  • Thank you! That got me closer.

    I’m still seeing the output buffer at the top of the page (although the URLs are correct, now), and only one row (post) is showing, without the permalink where it should be. Any idea why all of the rows aren’t showing up, except in the output buffer (showing the permalinks) at the top of the page?

  • I have no idea why your seeing an output buffer. You are not using an output buffer or actually outputting anything in your code. Something else is causing it and not something in the code that you’ve posted.

  • Two things were wrong (three, counting the missing global $post; line) :

    1) I was using the_permalink() instead of get_the_permalink(), and that was causing the output buffer.

    2) I had to append each row to a final output variable. So I added this at the end of the while loop: $final_output .= $output; Then this at the end to return: return $final_output;

    Thanks for your help John!

  • ah, sorry, I missed the use of the_permalink() which of course causes output.

  • Without the global $post; I wouldn’t have been able to figure it out. I’m curious why that is needed in this format, but not if I were putting it directly on the page, like this:

    <?php if ( have_rows( 'team_members' ) ) : ?>
    	<?php while ( have_rows( 'team_members' ) ) : the_row(); ?>
    		<?php $post_object = get_sub_field( 'team_member' ); ?>
    		<?php if ( $post_object ): ?>
    			<?php $post = $post_object; ?>
    			<?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; ?>

    Does that have to do with the shortcode vs. on page code?

  • All template function in WP define the global variable making it available in all templates loaded by WP.

  • Right. Makes sense. Thanks!

    For anyone who might stumble on to this, here’s the final code I ended up using:

    function get_team_members() { 
    	
    global $post;
    	
    if ( have_rows( 'team_members' ) ) { 
        $output = '<div id="team-grid">';
    	while ( have_rows( 'team_members' ) ) : the_row(); 
    		$post_object = get_sub_field( 'team_member' );
    		if ( $post_object ) {
    			$post = $post_object;
    			setup_postdata( $post );
                $headshot = get_field( 'headshot' );
    				$output .= '<div class="team-member">';
                        $output .= '<a href="' . get_the_permalink() . '">';
                            if ( $headshot ) {
                                $output .= '<img src="' . $headshot['url'] . '" alt="' . $headshot['alt'] . '" />';
                            }
                        $output .= '</a>';
                        $output .= '<div class="team-member-info">';
                            $output .= '<div class="team-member-name">' . get_field( 'name' ) . '</div>';
                            $output .= '<div class="team-member-title">' . get_field( 'title' ) . '</div>';
                        $output .= '</div>';
                    $output .= '</div>';
    			wp_reset_postdata();
    		}
    	endwhile;
    	$output .= '</div>';
    }
    	
    // return data
    return $output;
    
    }
    add_shortcode( 'show-team-members', 'get_team_members' );
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.