Deprecated: The PSR-0 `Requests_...` class names in the Requests library are deprecated. Switch to the PSR-4 `WpOrg\Requests\...` class names at your earliest convenience. in /home/customer/www/techforluddites.com/public_html/wp-includes/class-requests.php on line 24
Applying Functions Only to Specific Pages in Thesis

Thesis: Applying Functions Only to Specific Pages

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.

function custom_text_box() {
?>
<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:

  1. This is the beginning of a function I’m calling “custom_text_box”.
  2. Now I’m going to switch from PHP to HTML. (?>)
  3. Here’s the HTML code for the box, with some CSS thrown in to give it some styling.
  4. Now I’m going to start using PHP again. (<?php)
  5. This single bracket means I’ve finished writing the function.
  6. 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.

function custom_text_box() {
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:

  1. This is the beginning of a function I’m calling custom_text_box.
  2. If this is the About page, this is what you should do.
  3. Now I’m going to switch from PHP to HTML. (?>)
  4. 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.

Elizabeth Kricfalusi

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!

  • 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/

  • 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.

1 2 3

Recent Posts

T4L Monthly Update: February 2019

CES 2019, FaceTime bug, streaming the Super Bowl, Wi-Fi calling for Android phones.

5 years ago

Top Tech Stocking Stuffers

Big-ticket electronics get all the attention, but these little extras are always appreciated.

6 years ago

Four Ways to Access Control Panel in Windows 10

Microsoft is doing its darndest to hide the classic Control Panel from Windows 10 users.…

6 years ago