Support

Account

Home Forums General Issues Displaying ACF Relationship Field in Template

Solved

Displaying ACF Relationship Field in Template

  • I’m building a site where the client wants to display four featured posts on the homepage in addition to the chronological posts that appear normally: one before the loop begins, one after the 3rd post, one after the 6th post, and one after the 9th post. Each of these featured posts will be styled differently.

    My plan was to create a field group for Featured Posts and set it on the Options page so it has a separate admin area. I’ve done this, but I’m having issues getting them to appear in the template. Each featured post is a relational field.

    I’ve added the options page in functions.php. I’ve selected the featured posts from the Options page. But I’m having trouble getting them to appear on the front end of the template.

    Here’s how I’ve called it in the template:

    <?php 
    
    $posts = get_field('featured_post_top');
    
    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; ?>

    I put this before the main loop begins, but nothing is appearing, and all the other images on the page are not showing up correctly.

    For each featured post, I’d like to display the post title, post link, and featured image.

  • Hi @labedzde

    You need to pass ‘option’ as the second parameter of the get_field() function to get the value from an options page. This page should give you more idea about it: https://www.advancedcustomfields.com/resources/get-values-from-an-options-page/. So, it should be something like this:

    $the_posts = get_field('featured_post_top', 'option');

    I hope this helps πŸ™‚

  • Thanks! That helped, but I’m still running into an issue. The featured post link appears, but how do I get the featured image to also appear?

    Also, the rest of the loop is now messed up and the post titles and featured images do not show up.

    Could there be an error where I was resetting the post data?

      <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
  • Hi @labedzde

    To show the featured image, I believe you can use the the_post_thumbnail() function. It should be something like this:

    <li>
        <?php the_post_thumbnail(); ?>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <span>Custom field from $post: <?php the_field('author'); ?></span>
    </li>

    Could you please share the full code here: https://gist.github.com/? Also, could you please share some screenshots of the result and let me know how you want it to be? If you can share the live URL, that would be great too!

    Thanks πŸ™‚

  • I’m developing locally, so I don’t have a URL to share yet. Here is a Gist with the full code for my index.php file:

    https://gist.github.com/bibliofille/56f2a29528b708a16db6a09c79ea5859

    The ACF relationship fields called after the main loop are working fine, but the one called before the main loop shows up properly, but messes up the loop and prevents that content from appearing.

    Here is a screenshot of the result of my index.php file. There should be a post title and thumbnail image under every small green line in the grid.

  • Hi @labedzde

    Could you please try to change the $posts variable to something else like $the_posts? Sometimes, $posts variable is used as a global variable by WordPress.

    Thanks πŸ™‚

  • That worked! Thanks for your help!

  • Hi,
    I want to display autor post by Acf relationship field for particular custom post. already have created ACF relationship for each post. now want to diaply this. how can I?

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.