0

Trying to write a shortcode to create a gallery but I am getting a parse error. My foreach loop needs correction as well as returning the correct output. Any help please?

function allphotos_shortcode(){

  $images = get_field('fl_gallery');

  if( $images ) {
      $output .= '<ul>';
      foreach( $images as $image ) :
              '<li><a href="' . echo $image['url'] .'"><img src="' . echo $image['sizes']['thumbnail'] . '" alt="' . echo $image['alt'] . '" /></a><p>' . echo $image['caption'] . '</p></li>'
         endforeach;
      $output .= '</ul>'
}

return $output
 }

add_shortcode('allphotos', 'allphotos_shortcode');

1 Answer 1

2

You're missing semicolons after </li>, </ul>, and $output. You also shouldn't be echoing inside string concatenation and not adding the <li> tag to $output. The below is your code with these fixes.

function allphotos_shortcode(){

  $images = get_field('fl_gallery');

  if( $images ) {
      $output .= '<ul>';
      foreach( $images as $image ) :
              $output .= '<li><a href="' . $image['url'] .'"><img src="' . $image['sizes']['thumbnail'] . '" alt="' . $image['alt'] . '" /></a><p>' . $image['caption'] . '</p></li>';
         endforeach;
      $output .= '</ul>';
}

return $output;
 }

add_shortcode('allphotos', 'allphotos_shortcode');
2
  • thanks Jacob, if I also remove the first echo in $image[url] it works as expected!
    – JoaMika
    Commented Aug 10, 2017 at 14:42
  • No worries. I updated my answer to remove the echo I missed. Commented Aug 10, 2017 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.