WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a query for calling images from a post category. I am trying to control it to only query 6 images. I have tried using php count, but its not working as expected. Any ideas of what I could do differently to achieve this.

This was also the only way "I" was able to also include a "rel" tag for lightbox. Please keep that in mind if you know of a better way to do this.

Thanks -

I tried using the code pre tags and it wasnt showing full code so here is a pastebin link http://pastebin.com/kjkJQPR7

share|improve this question

Add a 'posts_per_page'=>6 variable to the array that you're passing to your get_posts query.

share|improve this answer
I am not really familiar with these wordpress functions. I have some things in that code that can probably be eliminated/changed. I added the variable to the array but it has mixed results due to query_posts above it which is displaying the category for the posts. And there is a posts_per_page variable in that too. So there is some confusion going on. But I cant get the $args array to show without the query_posts above with the category variable in it. I tried adding the category variable to $args but it didnt work.... – Clayton Miller Aug 16 '12 at 17:14
I'm not sure what you're talking about. Don't confuse the issue further by messing with the query_posts. Just add 'posts_per_page' => 6 to the $args array. Nothing else to be done. – Otto Aug 16 '12 at 17:15
line 1. on pastebin has that already. your telling me to put it in the $args array - I did. It conflicts and does not give the correct number of images. – Clayton Miller Aug 16 '12 at 17:32
Here is the complete working code I have. I still think this could be cleaned up. - pastebin.com/TGvgAUyU – Clayton Miller Aug 17 '12 at 15:37
I think you're misunderstanding here. The first query_posts line is getting the posts, not the images. Then, you're looping through the posts and calling get_posts to get the images that are attached to each post. So the posts_per_page=6 needs to be passed into the get_posts call to get that number of images for each post. Are you trying to limit the number of images to 6 across ALL the posts you're looping through? Because then that would be different, and you'd need to change the code more substantially. – Otto Aug 17 '12 at 16:47

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.