WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. estevancarlos
    Member
    Posted 2 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): ?>
    
    <li>
    <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>
    </li>

    http://wordpress.org/extend/plugins/advanced-custom-fields/

  2. estevancarlos
    Member
    Posted 2 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
    Member
    Posted 2 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.

    eg

    <?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

    best,
    Owen

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic