0

I was wondering if anyone could help me out. I have a custom post type called associations. I have associated some custom fields to the post type using Advanced Custom Fields, 2 of which are Post Object fields. This allows me to show content from a post from another post type. I would like to display this information in the single-associations.php, I have followed the documentation on the ACF website but i am running into a problem in which the image from the post object available_products is not displaying this field is labelled "product_icon", the post object fields are as follows (coverage_highlights, available_products)please see my code below:

    <?php

$post_objects = get_field('coverage_highlights');

if( $post_objects ): ?>

  <div class="association-products-side-bar">

    <h3 class="text-center">Coverage Highlights</h3>


    <ul>
      <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
          <li>


            <a href="<?php the_permalink(); ?>">
              <?php the_title(); ?>
            </a>


          </li>
          <?php endforeach; ?>
    </ul>
  </div>
  <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;

/*
*  Loop through post objects (assuming this is a multi-select field) ( don't setup postdata )
*  Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question.
*/

$post_objects = get_field('available_products');

if( $post_objects ): ?>
      <div class="association-products-side-bar">
        <h3 class="text-center">Available Products</h3>
        <ul>
          <?php foreach( $post_objects as $post_object): ?>
            <li>



    <?php

                        $image = get_field('product_icon');

                        if( !empty($image) ): ?>

                                        <img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />

                                        <?php endif; ?>





                              <?php endforeach; ?>


              <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>







            </li>
            <?php endforeach; ?>
        </ul>
      </div>
      <?php endif;

?>
1

If you don't pass a post ID to get_field, it defaults to using the global $post. You need to pass the ID of each of the available_products posts:

$image = get_field( 'product_icon', $post_object->ID );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.