Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Creating a carousel with dynamic images from a custom field

  • Hi,

    I am trying to create a carousel with dynamic images from a custom field.

    I can create the custom field – something like a gallery – but I’m struggling with the next step, that is to show the images on the carousel via image gallery or even query loop.

    Do you have any tip or suggestion to achieve that?

    My goal is, since I have to make dozens of venue pages, I would like to make a block element that serves has hero header for all pages.

    Thanks!

  • Hi there.

    Just to understand correctly. You have a custom post type of venues or similar and you want to be able to upload photos for each venue and show those images via a carousel, correct? Are you using ACF for your custom fields?

  • Hi,

    I’m going to reformulate my question, explaining it better:

    I’m working on a custom post type (fado_house) and need to display a dynamic image gallery in a Block Element.

    What I have:
    I’ve created with Claude a custom meta box that allows users to add/manage multiple images for each fado_house post. The images are stored as comma-separated attachment IDs in a custom meta field.

    – Meta key: _fado_house_gallery
    – Data format: "123,456,789" (comma-separated attachment IDs)
    – Stored in: WordPress post meta

    What I need:
    I want to create a global Block Element (hero header) that displays these gallery images in a Carousel Block for each fado_house post.

    My question:
    What’s the recommended approach to display images from this custom meta field in a GeneratePress Carousel Block?

    I hope this clarifies!

  • Hi there.

    It’s a bit tricky this one! Let’s try this. Create a static image carousel. Set it up as you would like to appear. Upload some static images to test. We are going to need some code to hack into the carousel items and populate them with our gallery images:

    add_filter( 'render_block_generateblocks-pro/carousel-items', 'inject_gallery_styled', 10, 2 );
    function inject_gallery_styled( $block_content, $block ) {
        // Only run on fado_house post type
        if ( get_post_type() !== 'fado_house' ) {
            return $block_content;
        }
        
        // Get comma-separated attachment IDs from custom meta box
        $gallery_ids = get_post_meta( get_the_ID(), '_fado_house_gallery', true );
        
        if ( empty( $gallery_ids ) ) {
            return $block_content;
        }
        
        // Convert comma-separated string to array
        $image_ids = explode( ',', $gallery_ids );
        $image_ids = array_map( 'trim', $image_ids );
        
        // Build carousel items HTML
        $items_html = '';
        $count = 1;
        $total = count( $image_ids );
        
        foreach ( $image_ids as $image_id ) {
            $image_id = intval( $image_id );
            
            if ( empty( $image_id ) ) {
                continue;
            }
            
            $image_url = wp_get_attachment_image_url( $image_id, 'large' );
            $image_alt = get_post_meta( $image_id, '_wp_attachment_image_alt', true );
            
            if ( ! $image_url ) {
                continue;
            }
            
            $item_class = 'gb-carousel-item';
            if ( $count === 1 ) {
                $item_class .= ' is-active';
            } elseif ( $count === 2 ) {
                $item_class .= ' is-next';
            }
            
            $items_html .= '<div class="' . $item_class . '" aria-roledescription="slide" role="group" aria-label="' . $count . ' of ' . $total . '">';
            $items_html .= '<img decoding="async" src="' . esc_url( $image_url ) . '" alt="' . esc_attr( $image_alt ) . '" style="width:100%;height:500px;object-fit:cover;" />';
            $items_html .= '</div>' . "\n";
            
            $count++;
        }
        
        if ( empty( $items_html ) ) {
            return $block_content;
        }
        
        // Replace content between opening and closing tags
        $opening_tag_end = strpos( $block_content, '>' );
        $closing_tag_start = strrpos( $block_content, '</div>' );
        
        if ( $opening_tag_end === false || $closing_tag_start === false ) {
            return $block_content;
        }
        
        $new_content = substr( $block_content, 0, $opening_tag_end + 1 ) 
                     . "\n" . $items_html 
                     . substr( $block_content, $closing_tag_start );
        
        return $new_content;
    }

    This should populate gallery images from each fado_house post and will only affect galleries appearing in this particular CPT. If you want to affect the styling of the gallery images, this part of the code affects them:

    style="width:100%;height:500px;object-fit:cover;"

    Hope it works the first time, let me know!

  • Hi,

    Thanks a lot George for your great and generous support!

    It worked perfectly.

    Best regards,
    Ricardo

  • Oh nice! Ok, no problem at all.

  • Hi.. Cool use of the carousel Ricardo! Can I chime in with a question ? Is this block only available in the beta release of Blocks Pro? Is there an ETA for it in the GPBP? It’s not showing in my settings so I ask. Thanks!

  • Hi @opus,

    The Carousel block was introduced in GB Pro 2.5. If you don’t see it available, your site is likely still using V1 blocks, as the Carousel block is only available in V2.

    To switch to V2, you can follow this guide:
    https://learn.generatepress.com/blocks/block-guide/generateblocks-1-x-to-2-0/#ver2

  • Hrm. I have Version 2.5.0 Go to Dashboard > GenerateBlocks > Settings ..but I’m not seeing the version toggle?

  • That means you’re already using V2 blocks. In the editor, do you see this list of blocks?

  • Sweet. Thank you.

  • No problem!

Viewing 12 posts - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.