javascript - Creating slideshow from folder with WP Bakery WordPress loop
I am migrating a client website orginally created in ASP into a WordPress driven CMS. The orginal site has about 200 separate directories, each with photos for a specific event and each directory is named with a unique number.
In the orginal site, there was an old (8 years +) JavaScript that would parse thru a given directory and create a Lightbox slideshow for it on the page (In additional, for each image within one of these event directories was a text file TXT with the same file name as each image which was used to create a caption - not that important, but worth mentioning). Each event directory has a different number of total images within each.
The goal will be to allow Editor users of the new WordPress site going forward to create gallery themselves using WP Bakery and the Image Gallery element, which is no problem.
My current challenge (and question) pertains to these past events...
I need to figure out:
How to incorprate a simple and current script (JavaScript) that can parse thru a given directory and return all the file names (ideally using the same lightbox as the WordPress/WP Bakery site does)
Write a WordPress loop to generate the corresponding WB Bakery slideshow code
So for example, I have manually uploaded via FTP a folder called 345 into the WordPreds Upload directory:
/wp-content/uplaods/show/345
In the dirctory 345 there are several images:
image01.jpg image02.jpg image03.jpg image04.jpg etc
Assuming at had a variable:
$LEGACYSHOWID = filename of the specfic show images directory (i.e. 345)
The code that WB Bakery genearted when a Slideshow is manually created is:
<ul class="slides">
<li style="width: 100%; float: left; margin-right: -100%; position: relative; opacity: 0; display: block; z-index: 1;" class="" data-thumb-alt="">
<a class="" href="https://DOMAINNAME/wp-content/uploads/SHOW/$LEGACYSHOWID/FILENAME01.jpg" data-lightbox="lightbox[rel-1949-3827996796]"><img class="" src="https://DOMAINNAME/wp-content/uploads/SHOW/$LEGACYSHOWID/FILENAME01.jpg-800x400.jpg" width="800" height="400" alt="FILENAME01" title="FILENAME01" draggable="false"></a></li>
</a>
</li>
<li style="width: 100%; float: left; margin-right: -100%; position: relative; opacity: 0; display: block; z-index: 1;" class="" data-thumb-alt="">
<a class="" href="https://DOMAINNAME/wp-content/uploads/SHOW/$LEGACYSHOWID/FILENAME02.jpg" data-lightbox="lightbox[rel-1949-3827996796]"><img class="" src="https://DOMAINNAME/wp-content/uploads/SHOW/$LEGACYSHOWID/FILENAME02.jpg-800x400.jpg" width="800" height="400" alt="FILENAME02" title="FILENAME02" draggable="false"></a></li>
</a>
</li>
(and so on...)
</ul>
I hope what I am asking makes sense. Can anyone offer any suggestions or point me in the right direction?
Answer
Solution:
you want to use the glob function. so add this to your functions.php
function get_images( $folder ){
$base_directory = trailingslashit( get_template_directory() );
$directory = $folder;
$images = glob( $base_directory . $directory . '*.jpg');
$output = array();
foreach($images as $image) {
$url = get_theme_file_uri($directory.basename($image));
$output[] = $url;
}
return $output;
}
then in your theme:
<?php $images = get_images('345/'); ?>
<?php if( $images ): ?>
<ul class="slides">
<?php foreach($images as $image): ?>
<li>
<a class="" href="<?php echo esc_url($image); ?>">
<img class="" src="<?php echo esc_url($image); ?>">
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
based on your comments to get the specific folder for each iteration:
function get_images_from_directory( $folder, $directory ){
$output = array();
$upload_dir = wp_get_upload_dir();
$base_directory = trailingslashit($upload_dir['basedir'] . '/' . $directory);
$images = glob( $base_directory . $folder . '*.jpg');
foreach($images as $image) {
$url = untrailingslashit($upload_dir['baseurl']. '/' . $directory . $folder . basename($image));
$output[] = $url;
}
return $output;
}
then in the theme:
<?php $folder = '110/'; ?>
<?php $directory = 'shows/'; ?>
<?php $images = get_images_from_directory($folder, $directory); ?>
<?php if( $images ): ?>
<ul class="slides">
<?php foreach($images as $image): ?>
<li>
<a class="" href="<?php echo esc_url($image); ?>">
<img class="" src="<?php echo esc_url($image); ?>">
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
Source