Search code examples
javascriptphphtmllightgallery

Lightgallery not working when adding h1 tag?


I am using Lightgallery and everything works, except when I echo an <h1> tag inside a foreach loop. When I remove it, the gallery works fine.

The only issue when I add the <h1> tag is that the images are not being loaded. I just see the preloader loading forever. What could be causing this?

My code:

<div id="lightgallery" style="border-top: 2px solid rgb(230, 230, 230);">
    <?
    $dir = $_SERVER['DOCUMENT_ROOT'].'/images/PROJECTEN/';
    $folders = array_diff(scandir($dir), array('index.html', '.', '..'));

    function scan_dir($dir) {
        $ignored = array('.', '..', '.svn', '.htaccess','index.html');

        $files = array();
        foreach (scandir($dir) as $file) {
            if (in_array($file, $ignored)) continue;
            $files[$file] = filemtime($dir . '/' . $file);
        }

        arsort($files);
        $files = array_keys($files);

        return ($files) ? $files : false;
    }

    foreach($folders as $gallerypart){
        $nounderscore = str_replace('_', ' ', $gallerypart);
        $gallery .= '<h1>'.$nounderscore.'</h1>';
        foreach(scan_dir($_SERVER['DOCUMENT_ROOT'].'/images/PROJECTEN/'.$gallerypart.'/') as $entry) {

            $gallery .= '
                <a href="http://www.website.nl/images/PROJECTEN/'.$gallerypart.'/'.$entry.'">
                    <img class="galleryimg" src="http://www.website.nl/images/PROJECTEN/'.$gallerypart.'/'.$entry.'" />
                </a>';
        }

    }
    echo $gallery;
    ?>
</div>

This is the part that breaks it:

$gallery .= '<h1>'.$nounderscore.'</h1>';

Solution

  • I fixed it by instead of using the standard markup, I used a selector.

    Like this:

    <script type="text/javascript">
        $(document).ready(function() {
            $('#selector1').lightGallery({
                selector: '.item'
            });
        });
    </script>
    

    And then inside the loop I added the following code:

    $gallery .= '
    <div class="item" data-src="http://www.website.nl/images/PROJECTEN/'.$gallerypart.'/'.$entry.'">
        <img class="galleryimg" src="http://www.website.nl/images/PROJECTEN/'.$gallerypart.'/'.$entry.'" />
    </div>';