Take the 2-minute tour ×

I'm creating a simple slideshow on the side where i want to show 4 images from a post, i searched around i found this code that simply uses the build in gallery function

<li><?php echo do_shortcode('[gallery id="'.$post->ID.'"]'); ?></li>

the problem is that it spits out the entire gallery... is there noway to limit this to 2,3 ?

i tried to use


but for some reason, i didnt work... I guess it because all my posts are made of is a gallery of images

all help is appreciated regards

share|improve this question

migrated from stackoverflow.com Sep 21 '11 at 8:28

This question came from our site for professional and enthusiast programmers.

you can check Gallery function in gallery plugin an can modified it as per your requirement or may create a new one which fulfill you needs !! –  Rikesh Sep 20 '11 at 11:14
but this functions is in its core, where do i find it to make changes to it –  Breezer Sep 20 '11 at 12:28
copy code from core gallery shortcode, then remove_shortcode('gallery'), then add yours (using add_shortcode('gallery','my_gallery_shortocode')); –  Maciej Kuś Sep 21 '11 at 8:49
hmmm ill check that out Maciej thanks –  Breezer Sep 21 '11 at 9:51

2 Answers 2

up vote 2 down vote accepted

There is no way to limit the gallery query in the shortcode_gallery function. However, you can get the child attachments of a post using the following.

$attachments = get_children( array('post_parent' => $post->id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby, 'numberposts' => 5) );

You will need to iterate through the attachments and build out your gallery HTML.

share|improve this answer
this was really simple imo and i got it work right away thanks alot ;) –  Breezer Sep 21 '11 at 11:02

To exclude an image from the WordPress Gallery you would use something like this:

[gallery link="file"  columns="4" exclude="2092,2091,2068"]

Here's a breakdown of the code above:

gallery link="file"

This links the image to the image file instead of the attachment post. Useful if you plan on using a thickbox or lightbox effect).

columns="4" This controls the images per row.

exclude="2092,2091,2068" This excludes the images that have the id matching the numbers used. There might be other ways to get the number ID for the images, but the way I get them is to go to your Media Browser by clicking on the Medial Link in the left side tool-bar. Next click on an image you want to exclude. You will see it's number ID in the URL of your browser.

share|improve this answer
yes but when u have a gallery of 20 images you would rather have it just limit the output then exclude 18 out them –  Breezer Sep 21 '11 at 9:38
If that's the case, then why even use the gallery. You could simply use the image ID's or the actual image URL to display them. If your trying to display a random 4 images of a given post, maybe using the gallery short-code isn't the best route. It was your idea to use the gallery, I just showed you how you could do it ;} –  Jeremy Jared Sep 21 '11 at 9:52
as i am fairly new to wordpress I'm trying whatever is given to me, if you read my question you'll see that I've tried get_the_post_thumbnail but it didn't work I found the gallery shorttag and it actually showed the images I posted opposite to everything else i tried, and i took for granted it should be an easy task to limit the output. I now know it isn't. –  Breezer Sep 21 '11 at 10:56

Your Answer


