• Resolved WillxD

    (@willxd)


    Hello everybody, I don’t know if is possible get the number of the attachment of a gallery.

    I have the code for the total number of the images, but I need get the number of the current image.

    The code for the number of all the attachment on my post:

    <?php
    $id = $post->post_parent;
    $total_attachments = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND post_parent = $id");
    echo $total_attachments;
    ?>

    But I need to show like this: photo #x of 20 (where #x is the current number of the image on the gallery and 20 is the total number of items of the gallery).

    Thanks.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter WillxD

    (@willxd)

    I’ve just found solution to my problem. What I miss was to add the order number in the admin panel, and for this the row menu_order was 0.

    If someone has the same problem as me, here I leave the code.

    <?php
    $id = $post->post_parent;
    $total_attachments = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND post_parent = $id");
    $imgnum = $wpdb->get_var("SELECT menu_order FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND post_parent = $id");
    ?>
    
    Image <?php echo $imgnum;?> of <?php echo $total_attachments;?>

    Thread Starter WillxD

    (@willxd)

    I made a mistake. The correct code is:

    <?php
    $parent = $post->post_parent;
    $id = $post->ID;
    $total_attachments = $wpdb->get_var("SELECT COUNT(ID) FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND post_parent = $parent");
    $imgnum = $wpdb->get_var("SELECT menu_order FROM {$wpdb->prefix}posts WHERE post_type = 'attachment' AND post_parent = $parent AND ID = $id");
    ?>
    Image <?php echo $imgnum;?> of <?php echo $total_attachments;?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Get the number of item in a gallery’ is closed to new replies.