I know I actually released this Part 3 a while ago, but I’ve rewritten it in a way that I thought would be more helpful and specific to WordPress, so I’m re-releasing it. Before reading this, please catch up on Part 1 & Part 2.
Conditional Statements
In PHP we have the following conditional statements:
if statement – executes some code only if a specified condition is true
if…else statement – executes some code if a condition is true and another code if the condition is false
if…else if….else statement – selects one of several blocks of code to be executed
Basically, it’s straight forward. Within a statement, if a certain condition is true, then something happens. Otherwise, something else happens. We can continue adding else statements as many times as we need.
So, within our PHP statement, we first define the variable and then set up the conditional statements like this:
2) {echo "got it right!";} else {echo "try again";} ?>
Notice that you define your variable and then use a semicolon to close that line. Next comes our “if” statement and then the condition is in parentheses. Finally, the code that we want to execute is between curly brackets. Again, the line ends in a semicolon.
There is also another acceptable way to use the if statement.
You will often see it used the second way with WordPress’ is_home or is_archive functions which check to see if it is the home page or an archive page, respectively.
These conditional statements can be really useful if you want your site to do one thing on one type of page but something else on another type of page. Here is a list of the most commonly used statements in WordPress:
is_single() //for single pages or single posts
is_page () //for any type of page
is_category() //for any category page
is_archive() //for any archive page
You’ll notice each option has the parentheses after. Inside those parentheses are what we call parameters, and we can use either the page ID# or the title in single quotes inside for a specific page or the slug. So, here is an example of the most common ways you might use is_page :
is_page()
When any Page is being displayed.
is_page( 42 )
When Page 42 (ID) is being displayed.
is_page( ‘About Me And Joe’ )
When the Page with a post_title of “About Me And Joe” is being displayed.
is_page( ‘about-me’ )
When the Page with a post_name (slug) of “about-me” is being displayed.
If you want to use these statements to affect the results of more than one specific page, you’ll need to use what we call arrays.
Arrays
Arrays are away of calling more than one type all in one line of code. These are set up in a very similar way, but we need to state that we are going to be using more than one input by saying “array” first, like shown below.
is_page( array( 42, ‘about-me’, ‘About Me And Joe’ ) )
Returns true when the Pages displayed is either post ID = 42, or post_name is “about-me”, or post_title is “About Me And Joe”.
is_page( array( 42, 54, 6 ) )
Returns true when the Pages displayed is either post ID = 42, or post ID = 54, or post ID = 6.
You’ll notice that they are set up the same way. ID# don’t use any kind of quotes, but page titles and slugs do. You can use more than one type together, so it is fine to use one category ID# and then call the next category by name. Be sure to use a comma between each. You can find a full resource of conditional tags here.
So all of these can be used, as indicated above, to call a specific sidebar or a special footer on any specific page.
I hope you will keep following my PHP for WordPress tutorials.
Leave a Reply