• Resolved dgwoolley

    (@dgwoolley)


    Is it possible to create a Meow Gallery that picks images from the media library randomly? For example I’d like a gallery of 9 images (3 x 3) that are selected from the the media library, that changes whenever the page is refreshed. I do have Meow Gallery Pro.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Jordy Meow

    (@tigroumeow)

    Hi @dgwoolley,

    I made an example for you and added it into the documentation, here is the direct link to it: https://meowapps.com/meow-gallery-tutorial/#Randomize_images

    You can add this code using the Code Snippets plugin for example. You can also play with this code to adapt it more to what you would like to do 🙂

    Thread Starter dgwoolley

    (@dgwoolley)

    Thanks Jordy

    Some feedback on the Randomize Gallery code:

    I uncommented these 2 lines:

    if ( !isset( $atts['random'] ) || $atts['random'] !== 'true' )
    return false;

    Adding a gallery with this shortcode:
    [gallery ids="" layout="justified" random="true"]

    only lists the first 5 images that I uploaded to the media library.
    Changing the LIMIT to say 8, lists the first 8 images that were posted to the library.
    No randomization.

    Also my galleries from Lightroom using WP/LR Sync with for example this shortcode:
    [gallery layout="masonry" wplr-collection="2"]
    reports the MEOW gallery empty when the 2 lines of code above are uncommented.
    However if I include random=”true”:

    [gallery layout="masonry" random="true" wplr-collection="2"]

    The gallery displays all images in that collection.

    But all other WPLR collections report gallery empty – so they must all contain the random attribute.

    “ORDER BY 1” means sorting on the first column in the table which is the post ID.
    I removed that in your code, and the script selects another 5 images (always the same 5 images) so not random.

    Quite a bit of troubleshooting to keep track of! 😉

    BTW I tried to update the latest version of the plugin v3.5.5 now and got an error message:
    “Update Failed: Download failed. Unauthorized”

    Cheers
    Dave

    Plugin Author Jordy Meow

    (@tigroumeow)

    Sorry, the SQL request was the wrong one. It should be this:

    SELECT ID FROM $wpdb->posts WHERE post_mime_type = 'image/jpeg' ORDER BY RAND() LIMIT 5

    For the update, I guess that’s because the URL of your website has changed at some point. Remove the serial, save, re-enter it, save, then… update 🙂

    Thread Starter dgwoolley

    (@dgwoolley)

    Thanks a lot Jordy

    Now it’s working as it should. Brilliant!
    I managed to put a refresh button above the random gallery to load a new one.
    OK it reloads the page then goes to an anchor just above the gallery. I suppose one could use an input button using AJAX so the whole page does not reload, but have not got there yet.

    To exclude a range of IDs from the random Gallery, I used this SQL SELECT statement in your code:

    $ids = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_mime_type = 'image/jpeg' <strong>AND ID NOT BETWEEN 647 AND 672</strong> ORDER BY RAND() LIMIT 9 ");

    It’s a pity that the WP gallery shortcode does not provide for a range of values in the id, include & exclude attributes.

    Cheers
    Dave

    Plugin Author Jordy Meow

    (@tigroumeow)

    Hi Dave,

    I’m glad it works! Indeed, it is also possible to do it through Ajax, however, I haven’t documented this yet. If you are interested, we can talk about it later, and I will make the API for that a bit cleaner and simpler (now it is only used by the Gutenberg Block).

    The way we worked on the Random feature, you could also add a way to exclude IDs, but as you can guess, that makes everything much more complicated code-wise. Or you could simply do it in the same function you are using for Random, by adding a new parameter “skip=’627,672′ for example. You will then find the skip variable in the $atts, you can split (https://www.php.net/manual/en/function.split.php) that value, and use the two resulting IDs (627 and 672) in your SQL request.

    Cheers 🙂

    Thread Starter dgwoolley

    (@dgwoolley)

    Thanks Jordy

    BTW you should update this page:
    https://meowapps.com/meow-gallery-tutorial/#Randomize_images

    to show the ORDER BY RAND() instead of ORDER BY 1

    Cheers
    Dave

    Plugin Author Jordy Meow

    (@tigroumeow)

    Thanks, @dgwoolley, it was not updated. Just did it 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Random Meow Gallery’ is closed to new replies.