• Resolved kilianso

    (@kilianso)


    Hey there

    I’m trying to use your plugin to create a custom block that is fetching a lot of images in a certain upload folder and create a gallery out of it. Within the block-editor view in the backend, it’s working as expected. But if I’m trying to preview the block in the frontend, nothing happens. The whole block is not being outputted. Any ideas?

    Here’s the code i’m using:

    <?php 
        // custom block to create a gallery based on image folder in uploads
    
        $home_url = home_url() . '/website/wp-content/uploads' . block_field('imagefolder');
        $upload_dir = wp_upload_dir();
        $image_folder = $upload_dir['basedir'] . block_field('imagefolder');
        $files = glob($image_folder . "/*.jpg");
        
        foreach($files as $image_name) {
            echo ($files ? 
                '<div class="singlepost__media__image has-pswp">
                    <figure itemprop="associatedMedia" itemscope itemtype="https://schema.org/ImageObject">
                        <a href="'.$home_url.basename($image_name).'" itemprop="contentUrl" class="singlepost__media__image__link">
                            <img class="lazy" data-object-fit="cover" alt="'.$home_url.basename($image_name).'" src="'.$home_url.basename($image_name).'">
                        </a>
                    </figure>
                </div>'
            : '');
        }
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Luke Carbis

    (@lukecarbis)

    Hi @kilianso!

    It looks like there could be quite a number of reasons why your template isn’t working. Since I wasn’t sure which debugging steps you’ve been through, I tried recreating the template myself, and got it to work.

    Here’s the result: https://gist.github.com/lukecarbis/031096956421e0fa3b8389dc16eb6b6a

    There’s a comment on that Gist which should help you get up and running. Let me know if you have any other questions!

    Thread Starter kilianso

    (@kilianso)

    Hello @lukecarbis

    Thanks for the support and the gist.
    It’s perfectly working now! For some reasons, I have to add /website/ to the URL in order to make the images load. It’s where WordPress is installed in a subdirectory. I don’t know why I have to add it here, but at least it’s working.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Block Preview not working’ is closed to new replies.