Home Forums ACF PRO Custom Post Type ACF Field in Header


Custom Post Type ACF Field in Header

  • Hi there,

    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

    $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">';
    					echo '</span>';
    					if (get_sub_field('reviewer_credentials') ) :
    						echo '<span class="b-revcred">';
    						echo '</span>';
    					echo '<span class="b-revname">';
    					echo '</span>';
    					echo '</li>';
    	endwhile; echo '</ul>'; 
    else:  endif; wp_reset_query();  endif; // end of the loop. ?>
  • Hi @kikadesign

    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.

  • Thank you for your help, Keith! I’m not sure why I didn’t mark this as resolved. Very sorry and you are very appreciated!

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Custom Post Type ACF Field in Header’ is closed to new replies.