Support

Account

Home Forums General Issues comma separated relationship values

Solving

comma separated relationship values

  • Hi,
    I am using relationship field to relate Person CPT with Movie CPT.
    In movie CPT I am showing cast members inline, like this:

    Cast: Person1, Person2, Person3

    Issue: Output values are not comma separated.

    my code is:

    <?php 
    	$cast_member = get_field('cast_member');
    ?>
    	<?php if( $cast_member ): ?>
    		<span>Cast</span>:
    		<span>
                <?php foreach( $cast_member as $person ): ?>
    				<a href="<?php echo get_permalink( $person->ID ); ?>">
    					<?php echo get_the_title( $person->ID ); ?>
    				</a>
    		<?php endforeach; ?>
    		</span>
    	<?php endif; ?>
  • 
    <?php 
      $cast_member = get_field('cast_member');
      if ($cast_member) {
        ?>
          <span>Cast</span>:
          <span>
            <?php 
              $persons = array();
              foreach ($cast_member as $person) {
                $persons[] = '<a href="'.get_permalink($person->ID ).'">'.
                              get_the_title( $person->ID ).'</a>';
              }
            ?>
          </span>
        <?php 
        echo implode(', ', $persons);
      }
    
  • How to implode two individual fields:

    I also want to hide h2 if both fields are empty. I’m not sure where to put php tags?

    if (get_field('father') {
      $values[] = get_field('father');
    }
    if (get_field('mother') {
      $values[] = get_field('mother');
    }
    if (!empty($values)) {
      echo implode(', ', $values);
    }
  • That looks right to me, maybe I don’t understand what you’re asking.

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

You must be logged in to reply to this topic.