• Home
  • About T4L
    • Contact
    • Legal Stuff
  • Subscribe

Tech for Luddites

News, Views, and How-Tos for a Digital World

Thesis: Applying Functions Only to Specific Pages

Last Updated: October 17, 2014

Note: This post may contain affiliate links, which means if you end up buying something from the site it goes to, I may get a commission for the referral. If you are using an ad blocker, some product information and links may not display unless you whitelist techforluddites.com.

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.

    Filed Under: Blogging, Code Sample, How To, Thesis Tagged With: Facebook, Thesis, WordPress

    If you found this post helpful, maybe your friends will too (hint, hint)!

    To receive future blog posts in your Inbox, please fill out the form below.

    Individual Posts Weekly(ish) Digest Monthly Newsletter

Comments for this page are closed. You can provide comments or ask questions via email at feedback@techforluddites.com. Unfortunately I cannot guarantee that I will be able to reply to every question I receive, but I will try.

Featured Posts

Streaming Media Players: A Comparison Chart

A comparison of features between the Amazon Fire TV, Roku, Apple TV, and Google Chromecast, including price, supported content providers, and more.

T4L TOP TEN

Stop the Email Threading Madness

Learn how to change your email inbox to list messages in the order they come in.

Windows 10: Change the Default Programs for Opening Files

Learn how to choose which programs you want to use with different kinds of files, instead of the ones Microsoft wants you to use.

Amazon Fire TV Stick vs Roku Streaming Stick+: Which One Is Right for You?

A comparison of the main differences between these two popular and affordable streaming media players.

Send Your Cable TV Signal Wirelessly to Another Room

Product review and how-to for systems that let you get cable/satellite programming on additional TV sets even if there are no outlets nearby.

Post Photos and Videos to Instagram from Your Computer

If, like me, you spend more time on your PC than on your phone or tablet, you can still share content on Instagram.

Create Different Headers in Word

Step-by-step instructions for using section breaks so you can have different headers on different pages of your Microsoft Word document.

Replacing List Bullets with Images Using CSS

Replacing standard HTML list bullets with images can be a great way to tie them into your site’s overall theme and make pages more visually appealing.

Streaming Media: Frequently Asked Questions

Still have questions about how a streaming media player like the Roku, Amazon Fire TV, Apple TV or Google Chromecast works? Find answers here!

Send Messages to People You Don’t Know on LinkedIn

While there are several ways to do this, purchasing an individual InMail is often the best option—and the most difficult to find.

The Twitter Hashtag: What Is It and How Do You Use It?

Learn what this ubiquitous symbol means and how to make the most of it in your tweets.

Privacy Policy

Data collected through forms on this website is used only for the purposes stated up front, e.g. sending newsletters to subscribers who opt in, selling products or services, etc. Tech for Luddites does not share or sell data collected. Learn more from our privacy policy.

Ads and Affiliate Links

Tech for Luddites is a participant in a number of advertising and affiliate programs. This means that if you take certain actions (click through, make a purchase) from links on this website, T4L may receive a commission for the referral. Learn more.

Contact Tech for Luddites

T4L is not currently accepting guest or sponsored posts. To ask a question, share a tech tip, suggest a post topic, or provide general feedback on this site, please email info@techforluddites.com.

Copyright © 2021 Tech for Luddites. All rights reserved. Content may not be copied without written permission.

This Site Uses Cookies

Learn more about T4L's privacy policy.