Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Multiple Loops in One Template1:39 with Zac Gordon
When you need to pull several different types of content into one template you likely have to call more than one loop. When this happens, and we’re dealing with multiple loops, we use a special function: wp_reset_postdata().
When you need to pull several different types of content into one template,
you'll likely have to call more than one loop.
When this happens and we're dealing with multiple loops in one template,
we'll have to use a special function, wp_reset_postdata().
wp_reset-postdata()is a function that resets the WP_Query object() we use
when we're writing custom queries with WP_Query().
There are three different types of reset loops in WordPress.
Rewind_posts() resets the normal loop so that we can use it again on the same page.
Let's say, for example, that you wanted to use a list of recent blog posts
in one part of the page,
then display the title and excerpt of the most recent post somewhere else on the page.
You could use this function right before the second loop.
wp_reset_postdata() resets the WP_Query() object.
We have to add this to our code when we have two custom loops on a page
that use WP_Query().
wp_reset-query() resets the $wp_query() and global posts objects.
But we're not really going to get into this one here
because as WordPress says in the Codex, WP_Query is the easiest
but not preferred or most efficient way to alter the main query.
With our site, we only need to reset custom post queries
so we'll only be using the wp_reset_postdata function.
You need to sign up for Treehouse in order to download course files.Sign up