Single page: multiple post_requests

I am planning to create a template page for a Wordpress theme that will load 5 separate "modules" within a portion of the page content. These modules are for each one of them showing the last 4 posts of different categories (to work similarly to this ).

What's the best way to approach this for loops?

Since page.php will call the while_posts loop to execute the template, the template php itself will have five modules. I would normally do while_posts for the request, but doing it five times on the same page during while_posts seems like nothing but a nuisance.

Is there a more convenient way to achieve this?

+3


source to share


1 answer


Yes, the preferred way of doing multiple loops is to create a new WP_Query object for each loop. Do you know the has_posts function, etc., which you are using for loops now? They are just helpers. These methods also exist inside the WP_Query object, so you can have one loop for each object you create.

Read about it here: http://codex.wordpress.org/Class_Reference/WP_Query



get_posts

also comes to mind. But you won't be able to create real Wordpress loops using this function because it just returns messages in a numeric array.

+1


source







All Articles