Support

Account

Home Forums ACF PRO Post Object Example Title Not Working for Me

Helping

Post Object Example Title Not Working for Me

  • Hey Elliot!

    I was trying to use your Post Object examples, but the conditional you have kept grabbing the page’s title and not the post I had selected in the ACF field on the page’s edit screen.

    This ended up working well with help from this thread.

    	<?php
    	// get post object selected by admin on home page
    	$post_object = get_field('feat_recipe');
    	
    	if( $post_object ): 
    	
    	// override $post
    	$post = $post_object;
    	setup_postdata( $post );
    
    //	foreach(get_field('post_object') as $post_object):
    
    	?>
      <div>
      	<h2><a href="<?php echo get_permalink($post->ID); ?>"><?php echo get_the_title($post->ID); ?></a></h2>
      	<div class="recipe-teaser">Post Object Custom Field: <?php the_field('recipe_teaser'); ?></div>
      </div>
      <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    	<?php endif; ?>
  • Very helpful! It’d be nice if this post was noticed and that example in the official documentation was modified.

    Thanks for posting!

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

You must be logged in to reply to this topic.