Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to alter my query_posts. My homepage is currently displaying all posts from all categories at random (posts_per_page =>16)

The problem is, one of my categories is advertisements and I want to only show 2 advertisements per page. I want to keep the randomness, but sometimes I get way to many ads returned. I want to limit it to 1 or 2.

So how do I say get all posts from all categories but only 2 posts from category x.

Thanks

share|improve this question
add comment

2 Answers

That sounds outside the scope of what you're going to get from query_posts. If you have a custom template for your home page and you want to sprinkle in advertisement posts I would change the query_posts() that excludes the advertisement category, do a separate get_posts() for 2 posts in the advertisement category, and then put them after the fourth and eighth (or something like that) non-advertisement posts in the loop.

The other way would be to increase your posts_per_page by a few and count each advertisement post as you display it in the loop. If any come up after the second one, just skip them.

share|improve this answer
    
Skipping posts might give problems when you are on the second page: you need to know how much posts you skipped on the previous pages to know where to start here. Doing the main query without ads and adding them in later seems to be the best solution. –  Jan Fabry Apr 27 '11 at 15:49
add comment

Do it in your loop - check each post for the category it comes from. Keep a counter for this and when this reaches it limit do not show anymore.

Alternatively you could do two queries and merge them: http://wordpress.org/support/topic/multiple-queries-compiling-into-one-loop?replies=5#post-1920638

Not sure how this would affect the ordering of the posts.

share|improve this answer
add comment

Your Answer

 
discard

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.