Created
January 10, 2016 19:06
-
-
Save simbasounds/e9396a325f13822d5e32 to your computer and use it in GitHub Desktop.
Simple WordPress image gallery shortcode. Add to functions.php.
Images categorised as 'Gallery' will be fetched. Data attribute is for Featherlight lightbox.
This file contains 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 | |
//Uncomment to enable shortcodes in widgets | |
/* add_filter('widget_text', 'do_shortcode'); */ | |
// Add Shortcode for Image Gallery Widget | |
function image_gallery_shortcode() { | |
ob_start(); | |
$args = array( | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'orderby' => 'rand', | |
'order' => 'desc', | |
'posts_per_page' => '9', | |
'post_status' => 'inherit', | |
'category_name' => 'Gallery' | |
); | |
$loop = new WP_Query( $args ); | |
while ( $loop->have_posts() ) : $loop->the_post(); | |
$thumb = wp_get_attachment_image_src( get_the_ID() ); | |
$large = wp_get_attachment_image_src( get_the_ID(),'large' ); | |
echo "<a href='' data-featherlight='" . $large[0] . "'><img src='" . $thumb[0] . "'></a>"; | |
endwhile; | |
return ob_get_clean(); | |
} | |
add_shortcode( 'image-gallery', 'image_gallery_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment