Last active
May 14, 2021 12:57
-
-
Save mapsam/b3afa4ada1e34bd2ccbb to your computer and use it in GitHub Desktop.
Wordpress ACF Lightbox
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Gallery styles available to add to your style.css */ | |
.gallery {} | |
a.gallery-image {} | |
a.gallery-image img {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This is the main gallery loop that finds an existing 'gallery' custom | |
// field within your page. This custom field should be created in your | |
// theme via the Advanced Custom Fields Gallery plugin. You can name it | |
// whatever you like, but be sure to update the 'gallery' in this file | |
// to what you named the field in your theme. | |
echo '<div class="gallery">'; | |
$gallery = get_field( 'gallery' ); // get the gallery object | |
foreach( $gallery as $img ) { // for each item in the gallery object, build a gallery thumbnail | |
echo '<a href="'.$img['url'].'" class="gallery-image" data-lightbox="gallery" data-title="'.$img['title'].'">'; | |
echo '<img src="'.$img['sizes']['thumbnail'].'">'; | |
echo '</a>'; | |
} | |
echo '</div>'; | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// include the lightbox JavaScript and CSS files in your header file. | |
// You can wrap them around a conditional if you want them to load | |
// only when you need them (i.e. only if the 'gallery' field has content) | |
// The lightbox.js file does all of the heavy lifting for you, no | |
// custom JS needed! | |
if(get_field('gallery')) { | |
echo '<link rel="stylesheet" type="text/css" media="all" href="'.get_stylesheet_directory_uri().'/PATH/TO/YOUR/SCRIPTS/lightbox.css">'; | |
echo '<script type="text/javascript" src="'.get_stylesheet_directory_uri().'/PATH/TO/YOUR/SCRIPTS/lightbox.js"></script>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you very much is easy and super