Home Forums Add-ons Gallery Field Returning Image Id's in Order from Gallery Field


Returning Image Id's in Order from Gallery Field

  • Hello all, I am trying to return the ID’s of the images that have been dragged an dropped into the gallery metabox on the post edit page. My client wants them in a specific order, which is the order she has sorted them in while uploading.

    I am trying to return the image id’s into a shortcode similar to this

    [justified_image_grid ids=<-- insert all images from gallery field here separated by a ',' -->]

    This way I can insert this shortcode into my theme and automatically have a gallery show up as it would if she had created the gallery in the regular gallery setup from wordpress, with all the id’s displaying in the order she sorted them into. Thanks!


  • SO just to explain – I can get this to return some of the other fields, such as caption, title, url etc. I just can’t get it to return the ID that would be used in a [gallery ids=(this is what I need to pull)]

  • Ok now I got it to return the id’s. . using:

    <?php $images = get_field('acf-gallery'); if( $images ): ?> 
    <?php foreach( $images as $image ): ?>
    <?php echo $image['id']; ?>,
    <?php endforeach; ?> 
    <?php endif; ?>

    The above gets me this output:

    <div class="textwidget"> 
  • What I need is this

    [justified_image_grid ids=315,314,305,306,308,309,307,313,311,312 ]

    How do I get it to put out a nice output like that, without the trailing “,”? Anyone? Thanks.

  • to get ids separated by “,” without trailing “,” you could use:

    <?php $images = get_field('acf-gallery'); 
    if( $images ): 
    $counter = 0;
    foreach( $images as $image ): 
    if ( $counter > 0 ) {echo ',';}
    echo $image['id'];
    endif; ?>
  • I can’t seem to get this code to work. . I do believe I need some formal PHP education! I’ll see what I can do, I just get no output from above. Thanks.

    – EDIT there were no images attatched to the first post I tried it on, my bad. IT worked great. Now how do I get those numbers into a shortcode for a gallery? ie

    [gallery ids"<--image ids-->"]

  • maybe that works:

    $blubb = '[gallery ids="';
    $images = get_field('acf-gallery'); 
    if( $images ): 
    $counter = 0;
    foreach( $images as $image ): 
    if ( $counter > 0 ) {$blubb .= ',';}
    $blubb .= $image['id'];
    $blubb .= '"]';
    echo $blubb;
  • This returns nothing at all? I am very greatful for your help, I realize my knowledge is pretty low on php, however it seems like I’ve tried many ways that I thought should work and don’t. Possibly I’ll find another more wise way to do this.

    I am open to suggestions for sure – the reason I’m doing this is I’m using another plugin called justified image grid, possibly it is the issue.

  • you need to check/correct this 2 lines

    $blubb = '[gallery ids="';
    $blubb .= '"]';

    and maybe you need to use do_shortcode instead of echo $blubb

Viewing 9 posts - 1 through 9 (of 9 total)

The topic ‘Returning Image Id's in Order from Gallery Field’ is closed to new replies.