Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Conditional Statements1:16 with Zac Gordon
Conditional statements let you test if a condition is true or false and then execute certain code based on if that condition is met. In this video, we introduce the topic of conditional statements and explain one of the ways they are commonly used in WordPress.
One of the primary reasons for
using a programming language like PHP is to write conditional statements.
Conditional statements let you test if a certain condition exists and
then execute code that is appropriate for that condition.
The simplest, and most common conditional statement you'll see in WordPress
is the if statement, or the if else statement.
A common use of the statement is to test if any posts are available for
a specific page.
If there are posts available, then you'll write out the code to display those posts,
using many of the template tags we looked at previously.
If there are no posts available we would use the else portion of the conditional
statement, and echo out a message saying there are no posts available.
Let's take a look at an example using some pseudo-code,
code written more in English than using real code.
This simple example will work for checking and displaying a single post.
But most of the time in WordPress, we combine conditional statements
with loop statements that let us apply our code as many times as necessary.
when you're looping through multiple blog posts on a blog listing page.
So let's take a look now at how we would combine a conditional statement
with a simple loop.
You need to sign up for Treehouse in order to download course files.Sign up