• Hello,

    I am using WordPress’s built-in gallery to show images below a “main” image. I want to display something like there are X images in the gallery on the main page only (index). Can someone help me do this? I’ve tried many codes that people pasted but none worked for me.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi, try this thread on StackEchange, I tested this code and it worked for me:

    <?php // Get the images attached to the post
    	$images = get_children( array(
        'post_parent' => $post->ID,
        'post_type' => 'attachment',
        'post_mime_type' => 'image',
        'orderby' => 'menu_order',
        'order' => 'ASC',
        'numberposts' => 999
    ));
    
    // If there are images, count them and display the number of images
    if ( $images ) {
        $total_images = count( $images );
        echo 'There are ' . $total_images . ' images in the gallery.';
    } ?>
    Thread Starter dredfunk

    (@dredfunk)

    Thank you very much Josh. Sadly in my case it counts the images incorrectly. Do you happen to know what could cause an incorrect counting?

    This is getting all the attachments for the post I think, so it will work on posts that only have a gallery of images, but if you have other images attached aside from that gallery, it might count those too? Is your count higher or lower than the number of images in the gallery?

    Thread Starter dredfunk

    (@dredfunk)

    I just found out that if I add a new post, upload new images and create a new gallery with these newly uploaded images the counting works. But the counting for older posts does not work. I even deleted an older post and its gallery as well and re-created it but it still counts as 1 image. Can anyone please help? What can cause this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Count number of images in a Gallery’ is closed to new replies.