• Resolved macrobber69

    (@macrobber69)


    I am trying to get a unique count of images in a given gallery (the default gallery). I would like to find both the total count of images as well as the current image.

    For example, Now showing image 3 of 16. I have tried using something like:

    $Gallery = get_the_title( $post->post_parent ); // this works fine $total_attachments = $wpdb->get_var(“SELECT COUNT(ID) FROM {$wpdb->prefix}posts WHERE post_type = ‘attachment’ AND post_parent = $Gallery”);

Viewing 9 replies - 1 through 9 (of 9 total)
  • First of all, you have a SQL injection vulnerability in your call to $wpdb: https://codex.www.ads-software.com/Class_Reference/wpdb#Protect_Queries_Against_SQL_Injection_Attacks

    Use this code instead to get the current gallery on an attachment page:

    $gallery = get_children( array( 'post_parent' => $post->post_parent, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'orderby' => 'menu_order ID' ) );

    This is how default pagination on attachment pages works.

    However your X out of N will not really work because galleries are not stored in the database, and all attachment pages have the default ordering as uploaded in the media page.

    You can’t know what order the image is because the attachment page doesn’t know what gallery you came from. A gallery is a shortcode inside of some content, that’s where the order of IDs is stored. None of the attachment pages will preserve neither the order of galleries nor the content of galleries when browsing and paginating.

    It needs so much hacking to make it sort of work that my head hurts now ??

    Thread Starter macrobber69

    (@macrobber69)

    Having chased this rabbit down the hole for several hours, you are correct. It is a royal, unholy pain. The default gallery does not work as I would have expected. If I could poll the mysql database, I would – but that doesn’t appear to buy me much. After several hours, I decided that maybe I could just figure out how to grab the shortcode IDS from the page, iterate through them to at least give me a count. But honestly, that seems like a very clunky solution and, as you noted, order is not preserved. It is such a small element in the overall design I am executing [Image 3 of 10] but it has exhausted my desire to care.

    Why not use Jetpack’s Galleries? That way you can customize everything in JavaScript and add whatever you need without using the standalone attachment pages.

    Thread Starter macrobber69

    (@macrobber69)

    Part of the design for the customer is the single-image attachment page (heavily modified, of course). This approach allows me to modify images.php / pages.php to accommodate the design. I was hoping to use the default gallery approach as I have already been forced to use two separate plugins for images in other areas of the design. When I asked the question, I had no idea that wordpress was so obfuscated it would make a fairly simple design element so challenging to solve.

    It’s not obfuscated, default galleries are VERY simple and there’s no flexibility to them, so is the attachment page. Indeed it’s challenging, because there’s no relation between an attachment page an a gallery, simple as that. parent_post can’t be different for two separate galleries. It’s just an inflexible simple implementation. An attachment page is not designed to know about the simple shortcode galleries, there’s simply no connection.

    Thread Starter macrobber69

    (@macrobber69)

    It’s interesting though, while I agree there is no relation to the gallery, because (I assume) the shortcodes are placed into the page, the attachment page ‘knows’ both Next and Previous images to display. So, it is able to devine ‘some’ relationship – even if there is no connection. As I said, I assume this is gathered from the IDS in the shortcode [gallery ids="729,732,731,720"] and thought that maybe, I could simply grab those and parse them (iterate through for a count and match for a location). That was my thought at least. But as is with most things, it becomes a law of diminishing returns.

    It’s not a divine relationship, the next and prev links use the Media gallery ordering, simple as that (unless you regroup them by post_parent).

    Hmm, now that I think of it (after a bit of sleep and more digging around) it is possible to do this in a completely reliable way, but it’s quite hacky.

    So here goes:

    1. Each gallery shortcode does wp_get_attachment_link, you need to hook into (wp_get_attachment_link filter) and tack on your post ID and maybe the gallery number (if there are more than one gallery in the post), for example /attachment.php?p=919191&post=1&gallery=1

    2. Inside your attachment template you can now use $_GET['post'] and $_GET['gallery'] (if you decide to use it) to backtrack your way.

    3. You are no longer allowed to use the default pagination implementation, but rather code your own (about 2-3 lines of code).

    All in all possible in about 15 minutes of tinkering.

    Thread Starter macrobber69

    (@macrobber69)

    Clever…I see what you are getting at here. I’m going to mark this as solved. Thanks Gennady, hacky is better than nothing!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Image Count from Individual Default Gallery’ is closed to new replies.