Relationship ACF snippet

Posted on: August 17th, 2022
By: Tadeo Martinez

This is the regular way of using the Relationship ACF. If this one doesn’t work, try the one below:

$featured_posts = get_field('page_links');

if( $featured_posts ):
foreach( $featured_posts as $post ): 
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post);
echo '<h6><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h6>';
echo '<hr class="mt-2">';
endforeach;
    // Reset the global post object so that the rest of the page works correctly.
    wp_reset_postdata(); 
endif;

Try this which doesn’t use the reset_postdata();

$featured_posts = get_field('locations');
if( $featured_posts ):
    echo '<div class="row justify-content-center">';
    foreach( $featured_posts as $featured_post ): 
        $permalink = get_permalink( $featured_post->ID );
        $title = get_the_title( $featured_post->ID );
        $custom_field = get_field( 'field_name', $featured_post->ID );
        // echo $featured_post->ID;
        // Get the ID of the featured image
        $featured_image_id = get_post_thumbnail_id($featured_post->ID);
        // echo $featured_image_id;
        // echo 'hello';


            // echo esc_url( $permalink );
            // echo esc_html( $title );
            //     echo esc_html( $custom_field );

            echo '<div class="col-md-6" style="padding:5px;">';
        echo '<div class="position-relative overflow-h img-hover">';
        echo '<a href="' . $permalink . '" data-lightbox="image-inventory" style="" class="d-block">';

        echo wp_get_attachment_image($featured_image_id,'full','',[
            'class' => 'w-100 d-block img-gallery',
            'style' => 'height:350px;object-fit:cover;'
        ]);

        echo '</a>';
        echo '</div>';


        echo '<div class="position-absolute bg-accent" style="top:50%;left:50%;transform:translate(-50%,-50%);mix-blend-mode:multiply;width:250px;height:40px;"></div>';

        echo '<div class="position-absolute text-white" style="top:50%;left:50%;transform:translate(-50%,-50%);">';
        echo '<span style="font-size:1.5rem;" class="bold">' . $title . '</span>';
        echo '</div>';

        echo '</div>';

          endforeach;
          echo '</div>';
endif;

Have any questions or comments? Write them below!


Leave a Reply

Your email address will not be published. Required fields are marked *