X

Limit number of images in a gallery

Just wanted to share this one. I had been struggling with it for a while. Feel free to use anywhere in a theme file, replacing ### with the ID of the page with the gallery!

Code Snippet:

<?php 
								
$posts_array = get_posts( array('post_parent' => '###', 'post_type' => 'attachment', 'numberposts' => 2) );

if ($posts_array) {
         foreach ($posts_array as $attachment) {
         	$ids .=  $attachment->ID.',';
         }
}
echo do_shortcode( '[gallery link="file" id="###" size="thumbnail" include="'.$ids.'"]' ) 

?>

Comments  Leave a Reply

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!