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.
add_action(‘thesis_hook_before_headline’,’custom_text_box’);
Line by line, this basically says:
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.
add_action(‘thesis_hook_before_headline’,’custom_text_box’);
So now you’re saying:
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.
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’).
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.
CES 2019, FaceTime bug, streaming the Super Bowl, Wi-Fi calling for Android phones.
Big-ticket electronics get all the attention, but these little extras are always appreciated.
When 240 characters just isn't enough...
When ten seconds just isn't enough...
Microsoft is doing its darndest to hide the classic Control Panel from Windows 10 users.…
View Comments
Ok but in the case of a newsletter that is costume to look like the original theme and i just want it to appear in my static page, how should i do it???? I am lost
Hi Susy.
Sorry for the delay in replying. I'm not sure what you're trying to do. You should be able to make functions work on a static page using the is_page() option, like the example in the post.
- Elizabeth
This is a very useful post thank you. I've been trying to figure out how to use openhooks one all pages except sales pages, so this is perfect, thanks Elizabeth!
Glad you found it helpful! - Elizabeth
Thank you so much, worked like a charm!
Thank you so much. I was stuck on this for weeks!
Glad you found it helpful!
I think it would be done like this
function custom_text_box() {
if (is_page('home')) {
?>
<?php
}
}
add_action('thesis_hook_before_post_box','custom_text_box');
right?
Hi Elizabeth
I wonder if you can help me. I want to know how to code a hook that appears
Before the first post: (thesis_hook_before_post_box) to add to my customs funtions.php
The important part is it must only appear on the home page.
Here is an example but it appears on every page that a post is open :
http://freeyourbeing.com/
How do you get a custom function to appear on every page?
How would I enable comments on just one particular page on my blog?
Wow this is a great tutorial. So with a bit of messing with the code i can add div columns with text, pics or banners to any page and any hook! Brilliant just what i was looking for. Thanks again.
okay sorry its late and had to much wine didn't read closely enough, fixed it now.
i guess i can add a class to the div and syle it custom css if i need to. Great thanks for this.