⚡Voxel Black Friday Sale is now Live

Count How Many Photos in Gallery Field

Description/Instructions

Count the number of photos in the gallery and display it on the front end.

If this snippet helped, feel free to buy me a taco :)

function count_gallery_images_from_gallery_field($atts) {
$atts = shortcode_atts(array(
'post_id' => get_the_ID()
), $atts, 'count_gallery_images_field');

$post_id = $atts['post_id'];
$gallery_field = get_post_meta($post_id, 'gallery', true);

if (empty($gallery_field)) {
return 'No images found';
}

$ids_array = explode(',', $gallery_field);
$count = count($ids_array);

return $count;
}
add_shortcode('count_gallery_images_field', 'count_gallery_images_from_gallery_field');

  • PHP
Copy Code

[count_gallery_images_field]

  • HTML
Copy Code

Let's Chat About this Snippet