I've created a post type, called Package (for news packages), and want to be able to link to specific blog posts and display them on the single-package.php page. I'm using ACF relationship fields, and using the post ID (tried with post object as well, but had the same problem)
The issue is that the title and permalink calls within my foreach statement aren't working. They're using the Package's title and permalink, instead of the posts title and permalink, despite using $item->ID in each call. Here's the code:
<?php while (have_posts()) : the_post(); ?>
<div class="entry-content">
<h1 class="content-headline">Package: <?php the_title(); ?></h1>
</div>
<div class="package-description" style="padding:15px 10px;background:white;">
<?php the_content(); ?>
</div>
<?php
$items = get_field('package_items');
?>
<?php if( $items ): ?>
<ul>
<?php foreach( $items as $item ): ?>
<?php setup_postdata($item); ?>
<li>
<a href="<?php echo get_permalink( $item->ID ); ?>">
<?php echo get_the_title( $item->ID ); ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endwhile; ?>
You can see the resulting output of the above code here.
Any thoughts?
$item
must be$post
, the codex page forsetup_postdata()
is quite clear on this point.