[Support request] Display relationship posts (ACF) after post content

Home Forums Support Display relationship posts (ACF) after post content

  • This topic has 5 replies, 2 voices, and was last updated 10 months ago by Tom.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1231569
    Fabien

    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 !

    #1231819
    David
    Staff
    Customer Support

    Hi there,

    can you try the after_content hook – and if possible share a link to a post where we can see this

    #1232356
    Fabien

    Hi,

    David same issue when using after_content.

    I’ve updated the topic with the URL. Please refer the div.test

    Fabien

    #1232460
    David
    Staff
    Customer Support

    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 ?

    #1324966
    Fabien

    Hi,

    I am still stuck with the same issue… maybe @Tom you have an idea ?

    #1325691
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    What does $posts contain, exactly?

    For example, if you var_dump($posts) after it’s defined – what does it output?

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.