Hello, please tell me how can I remove the last comma in the photo output loop
it’s my loop code:
<img class="location__image" src="<?php echo get_the_post_thumbnail_url(get_the_ID() , 'medium_large'); ?>"
data-hover-slides='<?php $location_gallery_urls = get_field('location_gallery'); ?><?php if ($location_gallery_urls): ?><?php foreach (array_slice($location_gallery_urls, 0, 5) as $location_gallery_url): ?><?php echo esc_url($location_gallery_url); ?>, <?php
endforeach; ?><?php
endif; ?>'>
Provide the beginning of the loop and information what it is that you are looping over in your foreach statement.
It requires using a counter, knowing how many items are to be looped over, and knowing when you’ve reached the end.
@hube2 Please, could you show me with an example of my code, the fact is that I barely understand what you are talking about, since I don’t understand PHP very well
<img class="location__image" src="<?php echo get_the_post_thumbnail_url(
get_the_ID(),
"medium_large"
); ?>"
data-hover-slides='<?php
$location_gallery_images = get_field("location_gallery");
if ($location_gallery_images):
foreach (
array_slice($location_gallery_images, 0, 5)
as $location_gallery_image
):
echo esc_url(
$location_gallery_image["sizes"]["medium_large"]
); ?>, <?php
endforeach;
endif;
?>'>
I will be very grateful to you! and even more today I will pray for you in church and light a candle)
In your code you are only showing 5 elements, so we know how many will be shown.
// add a counter at the beginning
$counter = 0;
// your loop
foreach ($items as $item) {
// output something for this item
// then
// increment counter
$counter++;
// if the counter is < output a comma
if ($counter < 5) {
echo ', ';
}
}