Search code examples
phpwordpressadvanced-custom-fields

Create WordPress gallery shortcode from Advanced Custom Fields


I am following the instructions set out in the ACF Gallery page at https://www.advancedcustomfields.com/resources/gallery/ in how to create a native Wordpress gallery using the shortcode and dynamically populating the item IDs from the data in the ACF Gallery field.

Example: EDIT: I have slightly modified the code to include definition for $image_ids

<?php

// Load value (array of ids).
$images = get_field('product_images');
$image_ids = get_field('product_images', false, false);

if( $images ) {

    // Generate string of ids ("123,456,789").
    $images_string = implode( ',', $image_ids );

    // Generate and do shortcode.
    $shortcode = sprintf( '', $images_string );
    echo do_shortcode( $shortcode );
}

Now my problem is that the an error returns saying that the value for $image_ids is undefined, and rightly so, can't see where that gets defined at all.

I've got some older code that I used to use:

<?php
// different product sizes (gallery)
if(get_field('product_images')) : ?>
    <h3>Product Images</h3>
    <?php 
    $image_ids = get_field('product_images', false, false);
    $shortcode = '[' . 'gallery ids="' . implode(',', $image_ids) . '"]';
    echo do_shortcode( $shortcode );
endif;
?> 

This code does work but returns an error php notice array to string conversion in jetpack-carousel. Yes I am using the 'Tiled Galleries Carousel Without Jetpack' plugin.

https://wordpress.org/support/topic/php-notice-array-to-string-conversion-in-jetpack-carousel/page/2/

I really want to follow the recommended way set out in the ACF documentation but it does not work for me. What am I doing wrong?


Solution

  • I don't have ACF installed but after reading their manual, I see a few issues with your code.

    Problem one: you are using both $images and $images_ids. Ultimately, both of them are doing the same job and that is to get back the ids array for you. Their data can be different from the last two arguments.

    Nonetheless, when you check to see if there is something, you check and evaluation with $images, but then you use $images_ids for your short_code string composition. For the error, I think that is because the version without the optional arguments returned a value while the one($images_ids) that utilized the optional arguments did not return a value but was being utilized anyway by the if clause.

    I think you kind of have to figure out, which get_field version you want to use. The one with 3 args or the one with 1 arg.

    This was edited after I noticed something, for why you are not getting anything, you may want to contact ACF to update their manual. That sprintf() statement will not do anything. It just going to generate a blank string that all. You may need to alter that sprintf() like the below to follow your old statement, if it still shows array to string conversion error, you would need to debug that.

    $image_ids = get_field('product_images', false, false);
    
    if( $image_ids ) {
    
        // Generate string of ids ("123,456,789").
        $images_string = implode( ',', $image_ids );
    
        // Generate and do shortcode.
        $shortcode = sprintf( '[gallery ids="%s"]', $images_string );
        echo do_shortcode( $shortcode );
    }