Simple WordPress image gallery shortcode. Add to functions.php. Images categorised as 'Gallery' will be fetched. Data attribute is for Featherlight lightbox.
<?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' );