Support

Account

Home Forums Front-end Issues Post Objects – Displaying Multiple Instances

Solving

Post Objects – Displaying Multiple Instances

  • I have created two multi-select Post Object fields that I need to display both of on the same page. I utilized the code from https://www.advancedcustomfields.com/resources/post-object/ to create my code utilizing my own HTML.

    The first instance renders on the page fine, but the second instance never renders. I can remove the first and then the remaining instance will render. What do I need to change with my code to make both instances work at the same time? Code below.

    <?php
    
    /*
    *  Loop through post objects (assuming this is a multi-select field) ( setup postdata )
    *  Using this method, you can use all the normal WP functions as the $post object is temporarily initialized within the loop
    *  Read more: http://codex.wordpress.org/Template_Tags/get_posts#Reset_after_Postlists_with_offset
    */
    
    $post_objects = get_field('display_products');
    
    if( $post_objects ): ?>								
    
     <div class="well">
      <section>
        <div class="row">
          <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <h2 class="term-heading">Products</h2>
          </div>
    
    <ul class="thumbnails list-unstyled">
        <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
    	<li class="col-xs-10 col-xs-offset-1 col-sm-3 col-sm-offset-0 col-md-3 col-md-offset-0 col-lg-3 col-lg-offset-0">
    		<article class="products type-products has-post-thumbnail hentry">
    			<div class="entry-summary">
    				<a title="<?php the_title(); ?>" target="_blank" class="" id="" href="<?php the_permalink(); ?>">
                 	<?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive')); ?> </a>
    			
    			</div>
    			<div id="entry-content">
    				<h4> <a title="<?php the_title(); ?>" target="_blank" class="" id="" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </h4>
    			</div>
    		</article>
    	</li>
        <?php endforeach; ?>	
    </ul>
        </div>
      </section>
    </div>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;
    
    ?>	
    				 
    <?php
    
    /*
    *  Loop through post objects (assuming this is a multi-select field) ( setup postdata )
    *  Using this method, you can use all the normal WP functions as the $post object is temporarily initialized within the loop
    *  Read more: http://codex.wordpress.org/Template_Tags/get_posts#Reset_after_Postlists_with_offset
    */
    
    $post_objects = get_field('display_resources');
    
    if( $post_objects ): ?>								
    
     <div class="well">
      <section>
        <div class="row">
          <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <h2 class="term-heading">Resources</h2>
          </div>
    
    <ul class="thumbnails list-unstyled">
        <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
    	<li class="col-xs-10 col-xs-offset-1 col-sm-3 col-sm-offset-0 col-md-3 col-md-offset-0 col-lg-3 col-lg-offset-0">
    		<article class="products type-products has-post-thumbnail hentry">
    			<div class="entry-summary">
    				<a title="<?php the_title(); ?>" target="_blank" class="" id="" href="<?php the_permalink(); ?>">
                 	<?php the_post_thumbnail('thumbnail', array('class' => 'img-responsive')); ?> </a>
    			
    			</div>
    			<div id="entry-content">
    				<h4> <a title="<?php the_title(); ?>" target="_blank" class="" id="" href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </h4>
    			</div>
    		</article>
    	</li>
        <?php endforeach; ?>	
    </ul>
        </div>
      </section>
    </div>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;
    
    ?>	
  • Hi KimFen, same issue here, were you able to solve it?

  • Hi KimFen and Hectormunozg.

    Same issue here.
    The wp_reset_postdata() is the magic to to this but it’s not working

  • My guess here is that you are running into issues caused by multiple nested queries. What is happening before this code is called?

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

The topic ‘Post Objects – Displaying Multiple Instances’ is closed to new replies.