0

I am making a artist portfolio site in wordpress. I am working on a single page for a custom post type Artwork, which contains numerous metadata fields created using ACF. I have run into trouble displaying the contents of an ACF repeater field with the names of collaborators, if there are any, who contributed to the artwork linked to their personal sites if they have one. This repeater is cloned as one element into the Artwork Field group.

I have been able to display the collaborators' names with and without links using the code below. So it will display "Co-creators: Name1 (with href)Name2 (no href)". But the commas don't show up between the collaborators. I think it is because I am not creating the arrays properly.

Any help would be greatly appreciated.

<?php
while (have_posts()) : the_post();
  // lots more code here
  $artwork_collaborators = get_field('artwork_collaborators'); // get the value of the acf clone field
  if ($artwork_collaborators) : // check if has data ?>
  <dd id="single-artwork-collaborators"><?php 
    _e('Co-creators: ', 'aopa'); // multilanguage label for translation
    foreach ($artwork_collaborators as $artwork_collaborator) : // foreach loop to get all collaborators
      if ($artwork_collaborator['artwork_collaborator_url']) : // check if there is a url for the collaborator
        $array_collaborators = array('<a href="'. $artwork_collaborator['artwork_collaborator_url'] . '" target="_blank">' . $artwork_collaborator['artwork_collaborator_name']  . '</a>'); // create array with URL
      else :
        $array_collaborators = array($artwork_collaborator['artwork_collaborator_name']); // create array without url
      endif;
      
      $collaborator_string = implode(", ", $array_collaborators); // implode the array and add commas
      echo $collaborator_string; // echo the collaborators with link or no link separated by commas if there is more than one
    endforeach; ?>
  </dd>
  <?php endif;
  // lots more code here 
endwhile?>

1 Answers1

0

I ended up getting some help on this problem from a programmer friend. Now the output is "Co-creators: Name1 (with href), Name2 (no href)" with the commas in the right place and the link in place when needed and not there when not.

One of the main problems was that the implode and the echo were inside the loop. This was causing them to repeat too many times.

We also created an empty array off the top, then added all the strings into that. See the comments in the code below:

  <dd id="single-artwork-collaborators"><?php 
    _e('Co-creators: ', 'aopa'); // multilanguage label for translation
    $collaborators_all = []; // Create an empty Array
    foreach ($artwork_collaborators as $artwork_collaborator) : // foreach loop to get all collaborators
      if ($artwork_collaborator['artwork_collaborator_url']) : // check if there is a url for any of the collaborators
        $collaborator = '<a href="'. $artwork_collaborator['artwork_collaborator_url'] . '" target="_blank">' . $artwork_collaborator['artwork_collaborator_name']  . '</a>'; // add rows in loop to the variable
      else : // check if there are any collaborators without URLS
        $collaborator = $artwork_collaborator['artwork_collaborator_name']; // add these rows in loop to the variable
      endif;
      $collaborators_all[] = $collaborator; // combine in the array created above all the rows of collaborator names, with and without and links, if there are any of each     
    endforeach;        
    $collaborator_string = implode(", ", $collaborators_all); // implode the array and add commas          
    echo $collaborator_string; // echo the collaborators separated by commas if there is more than one ?>
  </dd><?php 
  endif; ?>