Support

Account

Home Forums ACF PRO AFC gallery field displays featured Image

Solving

AFC gallery field displays featured Image

  • Hi there.

    I found a snippet online to display an ACF gallery.

    $blogLogos = get_field(‘blog_logos’);
    echo do_shortcode(‘[gallery ids="'.implode(',',$blogLogos).' size="full" link="none" "]‘);

    Unfortunately this also display the featured image.
    Is there a way to prevent this?

    Thanks for any pointer.

  • Hi @peternitras-be

    If you look at the docs for a gallery field, it may help.

    If you need to use a shortcode, here’s how the code should look:

    <?php
    
    // Load value (array of ids).
    $image_ids = get_field('gallery');
    if( $image_ids ) {
    
        // Generate string of ids ("123,456,789").
        $images_string = implode( ',', $image_ids );
    
        // Generate and do shortcode.
        // Note: The following string is split to simply prevent our own website from rendering the gallery shortcode.
        $shortcode = sprintf( '[' . 'gallery ids="%s"]', esc_attr($images_string) );
        echo do_shortcode( $shortcode );
    }
  • The issue that you may be having is that you are returning an array of image arrays or urls from the gallery. The WP gallery shortcode requires IDs.

    When you include a value for the “ids” attribute of the shortcode that WP does not understand then it reverts to its default behavior which is to show all images attached to the post.

    You need to set your image gallery field to return IDs or you need to get the unformatted value

    
    $blogLogos = get_field('blog_logos', false, false);
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.