Archived topic
Display relationship posts (ACF) after post content
5 replies · Started by Fabien on April 8, 2020
Hi,
Using GP elements, I am trying to display related posts (using ACF relationship field) below a single post.
In my element (before_footer), I've used a similar code as the one mentioned in ACF ressources (https://www.advancedcustomfields.com/resources/relationship/) :
<?php
$posts = get_field('relationship_field_name');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span>Custom field from $post: <?php the_field('author'); ?></span>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
However, it doesn't work, it displays data of my current post...
Should I add a wp_reset_postdata(); somewhere before ?
Thanks !
Hi there,
can you try the after_content hook - and if possible share a link to a post where we can see this
Hi,
David same issue when using after_content.
I've updated the topic with the URL. Please refer the div.test
Fabien
well the Hook is working as its displaying something .... but its impossible for me to tell why that code doesn't do what its meant to do.
You may want to check if you have any other functions are interfering the postdata query. Have you asked ACF if they have any insight to why it doesn't work ?
Hi,
I am still stuck with the same issue... maybe @Tom you have an idea ?
Hi there,
What does $posts contain, exactly?
For example, if you var_dump($posts) after it's defined - what does it output?