0

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

2 Answers 2

2

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.

1
  • 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
    Commented Apr 27, 2011 at 15:49
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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