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.