1. CYBER MONDAY SALE Get Divi.Help Pro Membership @ $99 / lifetime & instantly gain access to all our Divi.Help Pro Membership perks.LEARN MORE
Dismiss Notice
NEW Divi.Help Pro Carousel AI Toolkit - Magically turn your Divi design into carousel, simply by using our online toolkit. No plugins needed.Try our carousel AI toolkit
BEST Divi Block - A revolutionary drag & drop tool to easily mix & match 960+ premade blocks (Light & Dark) to kick start your Divi site design. Special module designs are included as well. Also newly added AI generator & color.
Learn More About Divi BlockFree Version ~ 340+ Free Blocks

Dynamic Content Gallery

Discussion in 'Free Divi Community Forum' started by Sebas-divi, Oct 12, 2024.

  1. Sebas-divi

    Sebas-divi New Member

    Hi,

    since it's not possible to create a dynamic content gallery within divi itself (why? ;-)) I try to build it on my own but it drives me crazy.
    I'm using Toolset and created a custom post field with the field-type: image.
    I allowed multiple instances of the field. So for example if I'm uploading 3 images, there are 3 instances of this field.
    The value of the field is the URL of the image.

    In the internet I only found solutions in conjunction with ACF-Plugin. But this is not a solution for me.
    I can't get over it with. Maybe someone have a solution. My code looks like that:
    (CSS-ID of Gallery-Module is "gallerie2")
    (My custom post field where imag-url's are stored is: "wpcf-post-bilder")

    function custom_toolset_gallery_ids($attrs) {
    // Überprüfe, ob das Divi Gallery-Modul verwendet wird
    if (isset($attrs['module_class']) && strpos($attrs['module_class'], 'gallerie2') !== false) {
    // Hole die Bild-URLs aus dem Toolset Custom Post Field
    $image_urls = get_post_meta(get_the_ID(), 'wpcf-post-bilder', false);

    // Wenn Bild-URLs vorhanden sind, setze sie in das Attribut
    if ($image_urls) {
    // Verwandle die URLs in eine durch Kommas getrennte Liste
    $attrs['gallery_ids'] = implode(',', array_map('esc_url', $image_urls));
    }
    }
    return $attrs;
    }
    add_filter('et_pb_module_shortcode_attributes', 'custom_toolset_gallery_ids');
     
    1. PRO MEMBER PERKS Divi Block Pro - Premade before / after image slider for your Divi site without using any plugins:View Demo
  2. Divi Booster

    Divi Booster Divi Expert

    Hi Sebas-divi,

    The code looks pretty close to what I'd expect to work. I think where it could be failing is here:

    $attrs['gallery_ids'] = implode(',', array_map('esc_url', $image_urls));

    The gallery_ids attribute should be comma-separated list of attachment IDs (i.e. the post IDs for the images in the media library) So something like "7,19,25". But it looks like you are setting it to a comma-separated list of URLs instead.

    I'd imagine you could get it working by either entering the attachment ID instead of the image URL in your custom post fields. Alternatively, you could modify your code to convert the image URLs into attachment IDs. I think the WordPress attachment_url_to_postid() function may do that:

    https://developer.wordpress.org/reference/functions/attachment_url_to_postid/

    Hope that helps.
     
  3. Sebas-divi

    Sebas-divi New Member

    Thank you very much for the hint. SOLUTION FOUND!
    For everybody else who have this problem here is the code which is working for me:

    function custom_toolset_gallery_ids($attrs) {
    // Überprüfe, ob das Divi Gallery-Modul die CSS-Klasse "gallerie2" hat
    if (isset($attrs['module_class']) && strpos($attrs['module_class'], 'gallerie2') !== false) {
    // Hole die Bild-URLs aus dem Toolset Custom Post Field
    $image_urls = get_post_meta(get_the_ID(), 'wpcf-post-bilder', false);

    // Wenn Bild-URLs vorhanden sind, setze sie in das Attribut
    if ($image_urls) {
    // Verwandle die URLs in eine Liste von Attachment-IDs
    $attachment_ids = array_map('attachment_url_to_postid', $image_urls);

    // Wenn die Konvertierung erfolgreich war, erstelle eine durch Kommas getrennte Liste der IDs
    if ($attachment_ids) {
    $attrs['gallery_ids'] = implode(',', $attachment_ids);
    }
    }
    }
    return $attrs;
    }
    add_filter('et_pb_module_shortcode_attributes', 'custom_toolset_gallery_ids');

    gallerie2 is my CSS-Klass of Gallery-Module
    wpcf-post-bilder is my custom post field made with toolset.
     
    1. PRO MEMBER PERKS Divi Mini Cart Pro Plugin - Instantly add 3 different types of mini cart to your Divi site:Learn More