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?
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; ?>
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.