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()
?>
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!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.