Skip to content

Snippets / PHP

PHP HTML

Count How Many Photos in Gallery Field

D Donald McGuinn · 253 views · Updated 1 year ago
Count the number of photos in the gallery and display it on the front end.
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 · 18 lines
[count_gallery_images_field]
HTML · 1 lines

Discussion 1

Ask a question or share how you used this.

Log in to join the discussion.
fmiguelgomes 1 year ago

Hi.... This is great but what if you have multiple galleries in the Post?

Voxel Guide assistant
Ask about snippets, addons & how-tos

Hey — how can I help?

I can dig through the library for snippets, addons, and tutorials.

AI can be wrong — double-check anything important.