Support

Account

Home Forums Front-end Issues Relationship Fields as string output

Solved

Relationship Fields as string output

    • erni41

    • August 29, 2013 at 12:55 am

    How can I output Relationship fields in one row (as string with comma seperated)?

    I have two CPT, CARS and CITIES and want to show the cities in the single-portfolio page of the cars post-type like this:

    Location: Boston, Paris

    Thank’s for your help

    • Elliot

    • August 29, 2013 at 2:20 pm

    Hi @erni41

    The relationship field will return an array of post objects. You can create a string representation of the post titles by looping through and echoing them out.

    Like so:

    
    <?php 
    
    $cities = get_field('field_name');
    $output = array();
    
    if( $cities )
    {
    	foreach( $cities as $city )
    	{
    		$output[] = get_the_title( $city->ID );
    	}
    }
    
    ?>
    <h3>Location: <?php echo implode(', ', $output); ?></h3>
    
    • erni41

    • August 29, 2013 at 8:59 pm
    This reply has been marked as private.
    • erni41

    • September 3, 2013 at 12:27 am
    This reply has been marked as private.
    • Elliot

    • September 3, 2013 at 2:33 pm

    Hi @erni41

    To answer your second question, please use a text field for any URL data.

    For your first, I don’t belivee I have yet read the email but can you check that the global $post->ID value is that of the correct post? Perhaps ACF is loading data from the wrong post?

    Thanks
    E

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.