I have a custom post type (books) with a repeater field (book_reviews), which then has 3 subfields. What I am trying to do is display all the reviews for all the posts in this custom post type. I have had no problem using the code (below) in the bodies of other sites, however, this site is different as I am trying to use the code in the header. I’m sure I am missing something simple but I cannot get the reviews to display when I put the code into the header (I’ve tried multiple ways of inputting the code including straight into header.php, creating a sidebar, and also using a template_part).
Here is the code I’m using
<?php wp_reset_query(); $my_secondary_loop = new WP_Query(array( 'post_type' => 'books', 'showposts' => -1, 'order' => 'ASC' ) ); if( $my_secondary_loop->have_posts() ): ?> <?php if( have_rows('book_reviews') ): echo '<ul>'; while( $my_secondary_loop->have_posts() ): $my_secondary_loop->the_post(); while ( have_rows('book_reviews') ): the_row(); // display the reviews echo '<li class="quotes"><span class="b-revs">'; the_sub_field('review'); echo '</span>'; if (get_sub_field('reviewer_credentials') ) : echo '<span class="b-revcred">'; the_sub_field('reviewer_credentials'); echo '</span>'; endif; echo '<span class="b-revname">'; the_sub_field('reviewer_name'); echo '</span>'; echo '</li>'; endwhile; endwhile; echo '</ul>'; else: endif; wp_reset_query(); endif; // end of the loop. ?>
Since you are outside the loop, you will have to also pass in the Post ID (using get_the_id()) when using: have_rows in the format:
while ( have_rows('book_reviews', get_the_id()) ): the_row();
However, with the above code… you cannot pass in the ID until after this line:
while( $my_secondary_loop->have_posts() ): $my_secondary_loop->the_post();
So… you will have to come up with different logic to check if there are any rows, and to wrap your unordered list, but I imagine this will get you on the right track.
Please mark as resolved or feel free to ask more q’s if needed.. I will subscribe to this thread.
You must be logged in to reply to this topic.
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!
Sorry for the issues with ACF's API SSL certificate this morning. We've resolved it and your activations and downloads should be back to normal again. Thanks! ^LG— Advanced Custom Fields (@wp_acf) November 2, 2022
© 2022 Advanced Custom Fields.