In The Basic Anatomy of a Custom Function, I showed you how to write a simple function that places a text box above your page headline. But you don’t always want certain content or functionality to apply to every single page on your site. In this post, I’m going to introduce you to conditional statements, which let you specify which pages your function will apply to.
I’m going to use the exact same example I used in the earlier post so, as a reminder, here’s the full code for that.
?>
<div style=”width: 400px; font-family: arial; font-size: 16px; font-weight: bold; color: #2361A1; padding: 12px; border: 3px solid #ccc; margin-bottom: 20px;”>
Isn’t this a nice custom function?
</div>
<?php
}
add_action(‘thesis_hook_before_headline’,’custom_text_box’);
Line by line, this basically says:
- This is the beginning of a function I’m calling “custom_text_box”.
- Now I’m going to switch from PHP to HTML. (?>)
- Here’s the HTML code for the box, with some CSS thrown in to give it some styling.
- Now I’m going to start using PHP again. (<?php)
- This single bracket means I’ve finished writing the function.
- Now take the contents of my function and put them on the page in the location called thesis_hook_before_headline.
Now I’m going to show you how to specify that you only want that content to appear on a single page of your site, using an About page as the example.
When you look at the non-code version of the code above, you might think you could simply add a line above step 6 to say, “If this is the About page, add this function.” (At least, that’s what I originally thought.) But that’s not how it works.
Instead, you have to put the If statement within the function itself and, when the add_action statement executes, it will apply those rules. This is how you write the code for that, with the additions highlighted.
if (is_page(‘about’)) {
?>
<div style=”width: 400px; font-family: arial; font-size: 16px; font-weight: bold; color: #2361A1; padding: 12px; border: 3px solid #ccc; margin-bottom: 20px;”>
Isn’t this a nice custom function that only appears on the About page?
</div>
<?php
}
}
add_action(‘thesis_hook_before_headline’,’custom_text_box’);
So now you’re saying:
- This is the beginning of a function I’m calling custom_text_box.
- If this is the About page, this is what you should do.
- Now I’m going to switch from PHP to HTML. (?>)
- Etc. etc.
Here’s how the About page and the Home page look when this function is applied.
About the syntax: Note the application of the “every open bracket must be closed” rule. First, in line 2, you’ll see there are two open round brackets—one at the beginning of the conditional tag and one when you’re specifying the page within the tag—and these are closed right beside each other, so you have two )s in a row. Also, since the If statement has an open curly bracket at the end of the line, you have the matching closing one at the end of the function. The second closing bracket goes with the opening one from the end of line 1.
What conditional tags are available?
This example is all very well and good if you want your own function to apply to your own About page. But what if you want it to apply to your Contact page or your Home page, or to every post in the Products category only, or on every page EXCEPT your About page, etc. etc.
(Apologies to AT&T…) There’s a tag for that!
This page in the WordPress Codex lists all the conditional tags available. I’m not going to repeat them all here, but I will give a few examples of common uses and constructions so you can use them as a guideline when you’re building your own.
- is_front_page() This will apply the function to your site’s home page, whether it’s a page full of blog posts or a static page.
- is_home() This will apply the function to your site’s home page only if it’s a blog page—not if it’s a static page. If you are using a static home page, is_home() will apply to whatever page you selected to be your blog page.
- is_page(’23’) This will apply the function to a static page with an ID of 23. If you’ve set your permalinks so that the page ID doesn’t show up in the URL, you can determine what it is by going to the WordPress Pages screen and mousing over the page name. The URL will show up in the status bar at the bottom left of the screen with the post-ID number in it.
Note: You can identify pages in a conditional statement using either the ID number, the page title, or the page slug, which is the last part of the URL when you’re not using default permalinks. For example, if the URL is www.techforluddites.com/products, then “products” is the slug, and the tag would be is_page(‘products’).
- is_single(‘5’) This will apply to the individual post page with the ID of 5. You can find the ID number by mousing over the post title in the Posts screen in WordPress.
- !is_page(’23’) The exclamation mark means “not” so this tag means the function would apply to any page (including pages with multiple posts on them) except for the one with the ID of 23.
- is_page(‘about’) || is_page(‘contact’) The double vertical bars mean “or” so this says to apply the function if it’s the About page or the Contact page (which means it will apply to both of them).
- is_single() && !in_category(‘business’) Here’s where you can see how you can build more complex conditional statements. The && represents “and”, so this is saying to apply the function to any page that is a single post page and that is NOT in the business category. In other words, every individual post page unless that post is in the Business category.
Note: There’s a difference between is_category(‘business’) and in_category(‘business’). The former applies only to the actual archive page for Business, which will list all the posts from that category. The latter applies to any post that you’ve assigned to the Business category.
There are many more combinations and permutations but even this short list will allow you to do quite a bit of customization to your site. If you have questions or problems with a specific conditional statement, feel free to send them to me and I’ll do my best to help you figure it out.