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.
Sign up for the Thesis newsletter to get more great tips, delivered right to your Inbox!Posted in Blogging,Code Sample,How To,Thesis

















{ 16 comments… read them below or add one }
Thank you so much.
I reckomend that everybody read the hole article so that you don´t do the mistake I did. Its a tricky thing about generic page ore not wich make it a bit difficult if you are a amature like me.
This is a very important thing if you have a ambition to increase the visability of your “home_page” on Search engine like Google. The blogname is some times h1 tag as a subject. and the text close to the subject should only appere in one page and not as the blog tagline that apperes on every page.
I will link to this page as a great library.
Thanks again.
I feel like I acquired a ton of knowledge reading this series. Thank you for taking the time to explain everything in such understandable terms.
I’m glad you’ve found it helpful. Please feel free to send any specific questions you have about Thesis to me at Thesis@TechForLuddites.com and I’ll do my best to answer them.
Hi, great tutorial Thanks!!
One question. How should I change the code if I want this box to appear below custom header and above content of specific page?
Can this be applied to category pages?
thanks, Gustavo
To change where the box appears on the page, you would change the hook named in the add_action line. Right now, it’s thesis_hook_before_headline. You would change that to wherever you want it to appear, e.g. thesis_hook_after_header or thesis_hook_before_content. You can find more info about hooks in this post:
http://www.techforluddites.com/2010/01/thesis-hooks.html
Yes, it can be applied to category pages. For example, if you had a category called “products”, you would change the conditional statement in the code to
if (is_category('products')) {Hope that helps.
Hi, nice tutorial. Only problem I have is I’m applying to a specific category. It’s working though the box it’s appearing before every single post on the archive. What should I put on the function before_headline so that the text box only appears once (at the post this category which happens to be an archive)? Thanks so much!! Gustavo
Did you try thesis_hook_after_header? That shouldn’t repeat multiple times for each post. You could also try thesis_hook_before_content_box or thesis_hook_before_content. Unfortunately I don’t have time to test it out right now, but will try in the next few days if one of these doesn’t work.
I have been trying to set up a wordpress website where the post on one of my pages is a little less wide than the post data on the other pages and I cannot for the life of me figure out how to set up the conditional to make that happen. I keep on getting random syntax errors. Any help?
This is the code that I am trying to set up as conditional:
<div class="post" id="post-”>
<?php the_content('Read the rest of this page »’); ?>
The idea would be to set it up a duplicate of this language so I can make the statement conditional for (is_page(’3′)), allowing me to go in and edit the CSS.
John, can you send me an email (Thesis@TechForLuddites.com) with a link to your site and the page you want to customize? It will make it easier for me to figure out what specific code you need.
Exactly what I was looking for – worked perfectly. Instructions and explanations are clear, easy to understand, and thorough.
This is a perfect example of how thesis tutorials should be done (and I’ve read plenty recently…)
Question, what if I just wanted to use thesis hooks and not go into the php file to make changes. For example, say I just wanted to word welcome to appear before the Navigation bar, but only on the home page? Can I use Thesis Hooks to accomplish this with a code? Thank you for this useful info!
I’m not sure I understand your question. A hook is just a place within the code where you can insert functions that do what you want to do. And you use the custom_functions.php file to write those functions. There is a plug-in called OpenHook that lets you insert code into hooks without going into the php file itself, but you can’t use it to add code to specific pages only. Here’s a post I wrote about OpenHook:
http://www.techforluddites.com/2010/01/thesis-openhook.html
I hope that helps.
this is pretty awesome post! thanks a lot :)
i have a question though..let’s say I want to you use is_page(”) conditional tag and specify more than one page id, is it possible to do that? What I want to do is apply certain theisis hooks to multiply pages (but not all of them) I have their page IDs but I have no idea how to use it with conditional tags
is it possible to do that?
Thanks again for your time. Your tutorial have already helped me a lot!
Howdy,
This is great. How do you modify the code, however, if you’re using Openhook? You would no longer need to define where the hook is placed since Openhook does that for you but you still need to code the conditional…how would that look using your example above for the About page but using Openhook to carry it out?
Thanks! Awesome stuff!
Thank you so much for your tutorial and going even further to explain the lines of code. It was extremely helpful and saved me a lot of time!!!!
This is very helpful. I have one question though, lets say that I want to target the hook itself to only one page or category, what would the function for that look like.
For instance, I’m trying to add a search bar to my website that only shows up on certain category pages.
The code for that hook is: add_action(‘thesis_hook_before_content_box’, ‘thesis_search_form’);
But I cant’ figure out how to place it into a function that targets only certain pages. Any ideas?
{ 3 trackbacks }