WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Display The Number of images in a post gallery (8 posts)

  1. HxC2123
    Member
    Posted 1 year ago #

    Hello,

    I'm trying to display the number of images in a gallery for each post. Each post will have a gallery and x amount of images in that gallery. I just want to show how many images are in each post gallery. Is this possible?

    Also, is there a way I can create a link to open the image gallery for each post? I don't want to insert the images into the post, I just want a the user to be able to click a link and be able to view the image gallery for that post.

    Thanks.

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    <?php
    $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
    if ( $images ) :
    $total_images = count( $images );
    $image = array_shift( $images );
    $image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );?>
    <p><em><?php printf( _n( 'This gallery contains <a %1$s>%2$s photo</a>.', 'This gallery contains <a %1$s>%2$s photos</a>.', $total_images, ),
    'href="' . esc_url( get_permalink() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to %s',  ), the_title_attribute( 'echo=0' ) ) ) . '" rel="bookmark"',
    number_format_i18n( $total_images )
    ); ?></em></p>
  3. HxC2123
    Member
    Posted 1 year ago #

    Thanks for the quick response.

    I got an error.

    Parse error: syntax error, unexpected ')' on line 45

    Line 45 is this line:

    `<p><?php printf( _n( 'This gallery contains %2$s photo.', 'This gallery contains %2$s photos.', $total_images, ),

    Thanks.

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Are you sure you added the code correctly? That was lifted straight from the Twenty Eleven theme.

  5. HxC2123
    Member
    Posted 1 year ago #

    Just did a copy and paste into my theme's index.php file.

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    Try:

    <?php
    $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
    if ( $images ) :
    $total_images = count( $images );
    $image = array_shift( $images );
    $image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );?>
    <p><em><?php printf( _n( 'This gallery contains <a %1$s>%2$s photo</a>.', 'This gallery contains <a %1$s>%2$s photos</a>.', $total_images, ),
    'href="' . esc_url( get_permalink() ) . '" title="' . esc_attr( sprintf( __( 'Permalink to %s',  ), the_title_attribute( 'echo=0' ) ) ) . '" rel="bookmark"',
    number_format_i18n( $total_images )
    ); ?></em></p>
    <?php endif;?>
  7. HxC2123
    Member
    Posted 1 year ago #

    Still get the same error.

    Parse error: syntax error, unexpected ')'

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    That code works for me as a drop-in.

Topic Closed

This topic has been closed to new replies.

About this Topic