Support

Account

Home Forums Add-ons Gallery Field Remove Last Comma

Solved

Remove Last Comma

  • Hello,
    I am creating a slideshow gallery, and as you can see in the code below I am pulling first image from gallery for the src and then the rest of the images for data-slide.

    <? $images = get_field('rec_gallery'); if( $images ): ?>
    	<img class="GalleryImg"
    	alt="<?php the_title(); ?>"
            src="<?php if( $images ): $image_1 = $images[0]; ?> <?php echo $images[0]['sizes']['large']; ?> <?php endif; ?>"
             data-slide="<?php $skip = true; if( $images ): foreach( $images as $image ): if ($skip) { $skip = false; continue; } echo $image ['sizes']['large'],','; endforeach; endif; ?>"/>
    <?php endif; ?>

    I need to separate the links I get for the images in data-slide with comma. The trouble I am having is that I end up with comma on the end of the last link.

    <img class="headerImg"
    	alt="Venison Sausage"
            src=" https://site.m/2018/04/25194824/stranger.jpg "
            data-slideshow="https://site.m/2018/04/29190427/DSC_5519-1024x683.jpg,https://site.m/2018/04/29190458/Confirming-Studies-1-1024x683.jpg,https://site.m/2018/04/29190502/Cunningly-Fomenting-1024x683.jpg,https://site.m/2018/04/29190505/Do-You-Like-Butter.jpg,"/>

    I can’t have the comma on the end because then I end up with one empty slide.

    Any ideas how to remove the last comma?

    Thanks.

  • 
    $images = get_field('rec_gallery');
    $slides = array();
    for ($i=1; $i<count($images); $i++) {
      // starting at 1 skips first image
      // use $i as index into $images 
      $slides[] = $images[$i]['sizes']['large'];
    }
    echo implode(',', $slides);
    
  • Thank you very much John

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

The topic ‘Remove Last Comma’ is closed to new replies.