4.4 If and Else
PHP if and else statements are absolutely critical for WordPress or PHP coding of any kind. In this part of our PHP WordPress tutorial, I'll show you how PHP if and else statements work and how to use them in the WordPress Loop.
1.Introduction1 lesson, 00:58
2.WordPress and PHP 2 lessons, 20:34
3.First Steps With PHP3 lessons, 28:09
4.Coding in PHP6 lessons, 1:06:20
5.Practical Project: A Custom Archive Page2 lessons, 21:07
6.Conclusion1 lesson, 03:28
4.4 If and Else
Hello, and welcome to this Tuts+ course on learning PHP for WordPress. In this part of the course, I'm gonna move on to looking at conditional statements in WordPress. And I'll talk you through if, else if, and else, and also through the conditional tags that WordPress provides you with. So you can see here from the PHP manual that there are three types of conditional statement that you can use. Firstly, if checks if something's the case. And then else if checks, if that isn't the case, if this is the case. And then, if that isn't the case either, you then have another else. Now, you could have as many else ifs as you want, depending on how many different scenarios that you want to check for. So for example, you could check for being on a static page, or for being in a single post. If statements in WordPress are really powerful. Firstly because without them, you can't run any sort of loop. And secondly because they allow you to specify when content or code is output, and when it isn't. So sometimes you might just want to hook something to an action hook in your theme on a single page or on a post archive. So the other thing you need to know about is conditional tags. WordPress has a whole load of conditional tags, such as checking if we're on the main page, or the front page, or the blog page. And you'll need to read that to get a full idea of which is which. And other things like admin, single post page, there are probably hundreds of them. So it's worth taking a look through this page on the codex and familiarizing yourself with them. And there'll probably be one or two that you used more often than others. So let's have a look at the code, here's the file that we worked on in the previous parts of the course. Now you can see that this has already got an if statement in it, if have_posts. But I'm gonna add another if inside that. So inside my article element, I'm going to add an if statement. I need to open PHP again. And I'll key that on the same line because I'm gonna be, Closing PHP here. So what I do need to do is make sure I put my closing bracket at the end. Right, so if I do that, that shows you what's inside my if statement. So what I'm doing here is checking whether the current post has a post thumbnail or a featured image. And if so, I want to output that, and I'm now coding in HTML So here I'm opening a section with a couple of classes, thumbnail, and align right. That align right, you need to make sure that that's set up in your style sheet so this will be floated to the right of anything else. I'm gonna close that section, and then within it, I'll display the post thumbnail, and again, I'm using PHP. So here I'm using a template tag called the post thumbnail. And what that does is output the post thumbnail for the current post. And I'm gonna set that parameter for that to medium, and that'll output the featured image in its medium size. So that's what happens if it has a post thumbnail. I then want to make sure that I've also got my content. So, I've got two sections, thumbnail and entry content. And I'm gonna make that float to the left, and again, you'll need to make sure you've got styling for that class in your style sheet. If the post thumbnail is in place, that's what will be displayed. I also need to display the title, and I'm gonna put that above both of them. So that's my h2, so if the post has got a post thumbnail, it'll output all that lot, but what if it doesn't? This is where an else statement comes in. So if that isn't the case, what I do is, I move this into the else statement. Here we go, I need to make sure, I've got an h2 class, entry title, PHP title, and then the content. Now I could add sections in here, so I can copy that so that it's the same. Making sure I close my PHP again and then I open it down here. And close it, because I've got this article here. We'll close the article tag in HTML, so let's indent the content so. If there's a post thumbnail, I'm outputting the title, and I'm gonna use class="entry title" there as well. Insert an h2 tag, then a section with a thumbnail, then a section with the content, and then I'm closing my if statement. If that's not the case, so else, there's h2 with the title, and the section with the content. So it's the same except for not showing the thumbnail. Now the reason I've included both the content and the thumbnail inside my statements is because of the classes being different. So this section here, I don't want to align left, because I don't need that floating. So you can see there how I'm using an if and else statement within my standard loop. Now let's have a look at this non-standard loop. Now here, I should have used an if statement, and I could throw an error if i don't. And the reason I need to do that is because I need to check if mypages actually fetches any pages. Because if I haven't created any top level pages in my site, it will fetch nothing and it'll throw up an error. So I need to check whether mypages is populated. Because that's a variable and not a function, you don't have to put brackets after it. So if that's the case, we'll echo out all that. Let's put the closing braces there, indent everything. So if mypages is populated, I'm then echoing out my unordered list, my full reach. And note that the unordered list is inside a conditional statement, but outside the full reach. And you might find yourself doing that if you were running a loop, for example, using WPQuery. You check if there are any posts and then you might output a heading for the full list of posts, and then you would start running the loop. Now another example of how I can use a conditional statement is to choose where in my site I want to run this function. So let's say I only want to output a list of the top level pages if I'm on another static page. So here, I add another conditional function, if is_page. And then again, I need to make sure that I'm closing that off, so let's indent everything here. There we go, so my function called rmcc_get_pages, first we'll check if we're on a static page. If we're not, nothing will happen, it will just go straight to the other end of my loop here, and then nothing will happen. If we are on a page, it will run all this code. So you can that you can then hook that to an action hook, so let's say, I'm gonna hook it to my compass_after_content hook in my theme. The name of the function is rmcc_get_pages. So it's important that you put your conditional statement inside your function. I've often seen people put conditional statements around these action hook calls. That just doesn't work if a conditional statement will only work inside your function. So you'll hook your function, whatever, but then if the conditional statement isn't met, the function won't actually run any code inside it, so it's not an issue. So that's how you add a conditional statement to check whether you're on a singular page, and then to output a list of top level pages if that is the case. So that's how if and else statements, and conditional tags work in WordPress. In the next part of the course, we're going to start looking at variables. And I'll show you how to assign variables in WordPress. See you next time, and thanks for watching.