Support

Account

Home Forums Front-end Issues Relationship Fields as string output

Solved

Relationship Fields as string output

  • 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

  • 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>
    
  • This reply has been marked as private.
  • This reply has been marked as private.
  • 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)

The topic ‘Relationship Fields as string output’ is closed to new replies.