Home › Forums › General Issues › WP Shortcode from ACF repeater field? › Reply To: WP Shortcode from ACF repeater field?
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' );
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.