Using Metabox Images Advanced field for individual image divs

Looking to see if anyone has successfully figured out a method for calling specific array items from a dynamic data source - specifically Metabox Image Advanced field. For example: trying to access - array item 3 of 5 for one of the divs.

Usage is for a gsap animation with multiple images. We want to pull in all five images added to a post gallery but only refer to specific ones of that Metabox field (in order to add certain classes for each). Will upload a screen shot for better reference.

Hey @FranklyCo,

when do you plan to “upload a screen shot for better reference”? :slight_smile:

However, a small helper function should do the trick:

function get_image_advanced( $id, $i = 0 ) {
    $images = rwmb_meta( $id );
    
    if ( ! is_array( $images ) || $i >= count( $images ) ) {
        return false;
    }

    return array_keys( $images )[$i];
}

You can then call this function like this:

{echo:get_image_advanced(my_custom_image,2)}

Backend:

Builder / Frontend:

Let me know if that helps.

Best,

André

2 Likes

Nice, @aslotta ! Yes, looks like this will do the trick! Thanks for sharing :wink: this looks awesome and opens up better class controls and less fields in the DB.

Here’s how we intend to implement this: Jam

@aslotta - I implemented the helper function to the child’s theme functions file - but it’s throwing DB connection errors… curious why it will render 1 or 2 of the gallery items but if all 5 are applied with dynamic tags, it malfunctions?

Assuming the function is only allowed to be called once on a page?





Metabox similar case and documentation: get advanced images from array - Meta Box

Should the full id be used since the field is in a MetaBox field group, ie:
{echo:get_image_advanced(mb_business_image_gallery_alt,0)}

instead of…

{echo:get_image_advanced(image_gallery_alt,2)}

Hey @FranklyCo,

if your field is wrapped in a group you probably have to adjust the custom function’s code. Works as expected for me fetching all the five images in an advanced image field:

Best,

André

1 Like

Okay, so after removing everything and then trying it again… it is working now so I’m confused as to why it was throwing errors. Guessing there was some cache conflict :thinking:

Will keep playing with it to see if it reproduces the same error. Super odd that it didn’t work the first time around…

Perfect timing! Haha yes, it seems to be working now too. The field is not wrapped in a group but under a MetaBox tab (that shouldn’t have any effect). Likely a builder cache conflict.

This should be added to the documentation :wink:

Many thanks @aslotta !! This is fantastic!

You’re welcome. Glad I could help. :slight_smile:

@FranklyCo And thanks a lot for the :coffee: !

1 Like