0

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 for setup_postdata() is quite clear on this point. – Milo Aug 26 '14 at 14:28
0

Use the old method of adding the relationship field -> Post Object

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

$posts = get_field('package_items');

if( $posts ): ?>

<ul>    

<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) 

    setup_postdata($post); ?>

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

<?php endforeach; ?>

</ul>

<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly

endif; endwhile; ?>
  • That's how I started originally, but it's the same problem. Even with the postdata setup, the results take the Package's name and permalink. I've removed the ID references and updated the page, so if you visit that page again, you'll see that it's the same :/ – tristanojbacon Aug 26 '14 at 10:46
  • Instead of using Relationship>Post Object field, try using Post Object directly .. this will solve your issue .. – Hassan Alvi Aug 26 '14 at 13:45
  • A good idea, but the template is now printing the post's ID AND the package title. See here for an example – tristanojbacon Aug 26 '14 at 13:51
  • I have updated the answer, please try it again .. – Hassan Alvi Aug 26 '14 at 14:19
0

In the posted code the variable $item is still being used but undefined now - also within the relationship query you can use standard WP calls like the_permalink() and the_title() without needing to echo.

  • Could you explain why $item is undefined at that point and what he should use in the place of $item ? – Howdy_McGee Dec 10 '14 at 16:42
0

I think he meant that the final foreach loop should look like this:

<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) 

    setup_postdata($post); ?>

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

As J Ivy mentioned, you just need to update the $item->ID to be $post->ID because you are no longer using $item in your foreach. You must use $post as pointed out by Hassan Alvi.

Also, thanks for posting your code, it helped me solve my problem too.

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.