Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ACF Single relationship link showing up as an array?

I’m using ACF with flexible content and within that a relationship field to display a single link on the logo. I’ve set the maximum links to one, so don’t need a loop to display multiple links, I just need the URL for one relationship link.

$clnk is the link and it needs to be displayed within the href tag, i.e.

<a href="' . $clnk . '"><img class="client_logos" src="' . $cl . '" alt="" /></a>

But the link itself is coming up as /Array ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help would be appreciated.

<h2>Our clients</h2>

<div class="services_container" style="display: block;">

    <?php
    // check if the flexible content field has rows of data
    if (have_rows('clients')):

    // loop through the rows of data
    while (have_rows('clients')) : the_row();

        if (get_row_layout() == 'clients'):
            $tl = get_sub_field('client_group_name');

            echo '<h2>' . $tl . '</h2>';

        endif;

    // Check rows repeater
    echo '<div class="clients_container">';

    if( have_rows('client_details') ):

        // Loop through rows.
        while( have_rows('client_details') ) : the_row();

            // Load sub field value.
            $cl = get_sub_field('client_logo');
            $cn = get_sub_field('client_name');
            $clnk = get_sub_field('client_link');
            
            echo '<a href="' . $clnk . '"><img class="client_logos" src="' . $cl . '" alt="" /></a>';

        // End loop.
        endwhile;

    endif;

    echo '</div>';

    endwhile;

    endif;
    ?>

</div><!--clients container-->

</div>

>Solution :

The official documentation for Relationship fields indicates that it will always return an array of the selected posts.

Adjusting your code to expect an array is simple enough (untested):

$clnks = ( array ) get_sub_field( 'client_link' );
$clnk  = array_pop( $clnks );

printf( '<a href="%s">', get_permalink( $clnk ) );

May want to consider changing the field type from Relationship to Link.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading