Support

Account

Home Forums General Issues Post Object Field doesn't work within a Function Reply To: Post Object Field doesn't work within a Function

  • I think you’ll have to pass along the current posts id as well (or use global $post)..

    
    <?php
    function example($field, $postID) {
         $post_object = get_field($field, $postID);
         
         if($post_object) {
              # get post data
    	  $post = $post_object;
        	  setup_postdata($post);
              ?>
                   <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
              <?php
              # reset postdata
    	  wp_reset_postdata();
         }
    }
    
    example('my_post_object', $post->ID); //in your template, assuming you do this inside the loop. If outside try replacing $post->ID with get_the_ID()
    ?>