Home › Forums › Add-ons › Repeater Field › Post Object inside Repeater only displaying first row › Reply To: Post Object inside Repeater only displaying first row
Your code would be easier to read if you used code tags.
Below is your code with code tags and comments explaining what the likely problem is.
<?php
// there is a main query that happens before this code is run
// this is a new custom query, not the main query
$args = array('post_type' => 'menu', 'posts_per_page' => -1, 'orderby'=>'menu_order','order'=>'ASC');
$menus = new WP_Query($args);
if ($menus->have_posts() ):
echo '<div id="the_menus">';
while ($menus->have_posts()): $menus->the_post();
$menu_name = get_the_title();
$menu_slug = $post->post_name;
echo '<div class="menu '.$menu_slug.'">';
if ( have_rows('appetizer_items') ):
// we need a way to restore the inner loop post
// this cannot be done with wp_reset_postdata()
// this will store the current $menus post
// so we can get it back later
$temp_post = $post;
echo '<h3>Appetizers</h3>';
echo '<div class="divider">'.$divider_dark.'</div>';
while ( have_rows('appetizer_items') ): the_row();
$appetizer_item = get_sub_field('appetizer_item');
$post = $appetizer_item;
setup_postdata( $post );
$title = get_the_title();
echo '<h4>'.$title.'</h4>';
// this next line is resetting post data
// to the main query post
// not to your custom query post
// you need to remove this statement
// wp_reset_postdata();
// and we don't need to restore the
// $menus post until after the end
// of the have_rows() loop
endwhile;
endif;
// end of have rows
// reset the inner loop post
setup_postdata( $temp_post );
echo '</div>';
endwhile;
echo '</div>';
// reseat the main query postdata
wp_reset_postdata();
endif;
?>
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.