Search code examples
relationshipadvanced-custom-fields

How to show only 2 post from post in ACF relationship


After creating customfield from ACF. I select multiple articles. How can I show only the last 2 posts? Thank!

Show only 2 post from relationship ACF


Solution

  • I would use the post_object field. With this field, you can select which objects you would like to display, when you configure it to "multiple values".

    <?php
    $featured_posts = get_field('featured_posts');
    if( $featured_posts ) { ?>
     <ul>
     <?php foreach( $featured_posts as $post ) {
        setup_postdata($post); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        </li>
    <?php } ?>
    </ul>
    <?php 
    wp_reset_postdata(); ?>
    <?php } ?>