Advanced Custom Fields
Unable to get URL from post_object (3 posts)

  1. estevancarlos
    Posted 3 years ago #

    I'm not sure what I'm doing wrong here. I have a field called "grid_thumbnail" and a "related_projects" post_object. I am able to display the title of the related post. I just don't know how to get the Image URL of that related post. Suggestions?

    <?php foreach(get_field('related_projects') as $post_object): ?>
    <img src="<?php echo get_field('grid_thumbnail'); ?>"><br />
    <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID) ?></a>


  2. estevancarlos
    Posted 3 years ago #

    How can I get a specific image field src from a related post, is basically what I'm asking.

    For example:

    <img src="<?php echo get_field($post_object->grid_thumb); ?>">

    That doesn't work. How can this be resolved?

  3. owen.hoskins
    Posted 3 years ago #

    Hi estevancarlos,

    In case you or future googlers do not find this on the AFC forms

    Hi guys,

    Here's some tips to get setup_postdata to work.
    1. when defining the foreach loop, the single item must be called $post.
    2. after the loop you need to use wp_reset_postdata(); to set the $post object back to normal.


    <?php foreach(get_field('related_post_items') as $post):
        setup_postdata($post); ?>
        <h2><?php the_title(); ?> - <?php the_field('something'); ?></h2>
    <?php endforeach; wp_reset_postdata(); ?>

    You can read more about it here: http://codex.wordpress.org/Function_Reference/setup_postdata


Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Advanced Custom Fields
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic