7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

  • Overview
  • Transcript

4.3 PHP in the Loop

In this lesson, you'll learn how WordPress PHP code runs the Loop. This is an important part of our PHP WordPress tutorial because the Loop is incredibly foundational to WordPress. It's the first thing that most new themers will modify... and you can get very creative with it once you know the basics.

Related Links

4.3 PHP in the Loop

Hello, and welcome back to this Tuts+ course on learning PHP for WordPress. In this part of the course, we're gonna move on to learning about the loop. And I'll show you how to code the standard loop in WordPress as well as a non-standard loop using the get pages function. So here's the Codex page on the loop, which tells you what you can include within it and how to use it. And here's a tutorial from Tuts+, giving you a beginner's guide to the WordPress loop. So let's have a look at how we code the WordPress loop. First, let's take a look at an example of a loop. And you might be familiar with this example because I showed you in the section on commenting. So this is a standard loop, and this is used in all the static pages in my website. So it starts here with if have_posts, while have_posts, The_post, and what that does is first it checks if there are any posts for the current content type. So that might be a single post or page, if that's what you're on, or it might be an archive. And then it says while we have posts, which is the point at which we start looping. So while there are posts to output, it will keep on looping through those posts. And in the case of a single page like this, that means it'll only loop once. But in the case of an archive, it'll keep looping until it runs out of content to show. And then the_post is where we set up the post and start working with it. So within that, there is a bunch of code which outputs content from the database relating to that particular post. And I'll show you that in a bit more detail shortly. So this loop is in an include file called loop-page.php. And that is called by my full width page template here, which is a custom page template that, again, you've seen before in this course, and also by my standard.page.php file. So you can see I'm using get_template_part to call in that include file. So let's code our own standard loop. So we open up our file, And I've got my commenting on there, that's better. Easy to get them the wrong way around but if you're using a good code editor, it'll show you that it's not working. So, Let's start with a standard loop. So we begin with that if have_posts, And then while have_posts, And then the_post. So that's our first line. Now, I'm gonna close off PHP now, so I can write some HTML. And I'm gonna indent, so I can see that all this is inside my loop. So I'm gonna open an article element. I can [INAUDIBLE]. So my article will have an id that corresponds to the id of this particular post. And then to set a class, I use a WordPress template tag called post_class. And then I close my article tag. Now I'm going back into PHP again. And I'm gonna write the HTML for the heading with the title of the post and the content. Making sure, again, that I spell that correctly. I made an error there. I don't have to open PHP again. I am still working in HTML. So I've got my opening article tag, my h2, followed by the content. And here I am using PHP. And then, finally, I want to close my article. So that's the content of my loop. So I'm opening up an article with the ID corresponding to the post ID, and with classes that will relate to the type of post it is, using the post class function. I'm then showing the title of this post in an h2 element and then outputting the content. And then, after doing all this, I need to end while and end if. Because I need to end these two conditional statements. One of them is while we have posts, and one of them is if. And I need to make sure I do while before if because it came after if in the opening line. So it's inside it. Making sure I open PHP again. And my good old code editor is telling me when I get that wrong. It's very easy to do. Okay, so there's my standard loop. So I'm checking if there are posts and I'm outputting them in a loop if I need them. Now let's have a look at writing a non-standard loop. And here, I'm gonna write a function which I could then add anywhere in my theme using a hook, or by typing in the function name. And within that, I'm gonna use the get_pages function provided by WordPress to get a list of pages. And what I'm gonna do is get the top level pages in the site, so I can create a list of all the top level ones. So there's my function. I'll start by setting some arguments for get_pages. And that $args is a variable, so it has a dollar sign in front of it. And my arguments are an array. And again, making sure it's typed correctly. So my arguments are that it should have no parent, so it's top level page in my site, sort_order is ascending, and the sort_column is menu_order. And that means within the back-end of my site when I'm editing those pages, I need to give them each a number that will correspond to the order in which they're being displayed. So now I'm adding get_pages. So I'm setting another variable called $mypages, which is what's fetched by the get_pages function with those arguments. And I'm gonna create a list with each of those pages as an item in that list. So there's my ul and I'm gonna close that ul as well. And here you'll see that I'm using echo instead of closing my PHP and opening it again. It's just to show you that there are multiple ways of doing this. And if you want it to work in HTML, you could close your PHP here. And then you will be able to code the HTML directly. Then within my unordered list, I use a for each loop. And that's contained in brackets. And then I have curly braces for my for each loop. And what I need to do is echo out a list item with a link and the title. So my for each loop is effectively the same as doing while on the post. So I'm running through each instance of my pages, calling at my page and then echoing all these out. So that creates my function. What I then need to do is hook that into an action hook or code it into my theme in order to get it to work. But that shows you how to do a non-standard loop. In the next part of this course, we're gonna move on to looking at conditional tags. And I'll use this code here to demonstrate to you how you can use conditional tags to display content in certain places. See you next time, and thanks for watching.

Back to the top