Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In our footer, we now have three areas that we can put content into. You can see them down here at the bottom. Recent Recipes, Recent Photos, and Recent Videos. Now, we want to add lists that display the title of and links to the five most recent items under each of these three custom post types. We're going to do that by creating a custom loop using a custom query. In our sidebar-footer.php file, we already have our dummy content set up. It's set up here under the unordered list where you can see there is an unordered list that has a list item and then inside the list item, there is an h3 header that has a link and the actual title of the post.
This is the code we'll keep using. What we need to do now is wrap this list item in a loop that goes to WordPress and says give me the five most recent items and then display them one at a time. In the code-snippets file, I've created this custom loop, but I've separated it from the sidebar content. The reason for that is you can now take this code and use it elsewhere on your site. The whole point of creating these custom loops is that you create kind of a closed loop where you can test for other things within a certain page.
So, for example, like we're doing, we can display multiple different loops in the footer or you could do the same in your regular sidebar or even inside a single post or a page template. If we copy out this code and paste it in, in place of the list item here, I'll explain to you what happens. It starts off by creating a variable called postslist. That's just so we can do a full search on it. The postslist variable is filled with the output from the function get_posts and it's here all the work is done.
You can give get_posts, which is a custom query, multiple variables that tell it what to search for. Right now, it says numberposts=4, which means we'll get four items returned. It's as to order them in descending order, order them by date, and then finally it says post_type=recipes. That means this will return 4 recipe items ordered by the date in descending order. So basically in reverse chronological order, the four latest items added to the recipes post_type.
Once the variable postslist has these items, we'll run a loop. The loop starts here, foreach, and ends down here, endforeach. What the loop does is it says foreach ($postslist, so this item up here, and we handle them as a post, we'll get the postdata for that post and then display it. So in effect, it goes in, picks a post out of the database, says give me all your data, and then display the_ permalink and wrap that around the_title.
When I save this and I reload the page, we now get links to the four latest items listed under Recent Recipes. Pizza Primavera, Jalapeno Jelly, Garlic Bruschetta, and Steamed Artichokes. Now that you've seen how it works, we can add the same batch of code to the two other boxes. So simply copy it out, go replace the list item, and then I just have to change the post_type here for photos, and I'll scroll further down under the same with videos.
When I reload the front page now, you'll see under Recent Recipes we have the four most recent recipes, under Recent Photos we have the four most recent photos, and under Recent Videos we have one video because we haven't added more yet. The cool thing about this is no matter where I go now, because this is in the footer, if I go to The Beauty of Food, the photo, and I scroll down to the bottom, we still see the same lists. If I go to Steamed Artichokes, again if you scroll down to the bottom, you still see the same lists.
Meaning that when people visit the site and get down to the bottom after
reading all your content, they will see that there is more content available on the site.
Like I said, this custom loop can be used almost anywhere on your site.
You just dump it into your templates, wrap it in an unordered list tag, so
to begin, and end to end, and then it will spit out whatever query you ask
it for and display it.
Simple and powerful!
Using a custom loop, we're able to call up content from other post types
anywhere in our site including in the sidebars.
Though this example used a footer, there is no reason why you can't do the same thing with the same code blocks in regular left or right sidebars or even in post or page templates. The possibilities here are only limited by your imagination.
Get unlimited access to all courses for just $25/month.Become a member
119 Video lessons · 49993 Viewers
117 Video lessons · 37300 Viewers
113 Video lessons · 81193 Viewers
65 Video lessons · 10610 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.