Tag Archives: Pages

Important New Article! Tutorial: Customizing Just Your Front Page, Pages or Posts Using Conditional Tags

How many times have you wanted to customize just the Home page, or just one page, or only posts? You’ve heard about Conditional Tags but don’t have a clue how to use them!

Well, you will after reading this new article!

Tutorial: Customizing Just Your Front Page, Pages or Posts Using Conditional Tags” provides all you need to know to be a master of Conditional Tags in your custom_functions.php file. Fully illustrated with code examples, it walks you through using Conditional Tags for the Home and Front pages, posts, and pages. It even shows you how to use CSS on selected pages!

This article will surely become one that you come back to again and again to get ideas for your customizations!

It is filed under the “Tips” section in a new grouping called “Code You Can Use.”

Tutorial: Customizing Just Your Front Page, Pages or Posts Using Conditional Tags

There are many times when Thesis users want to apply customizations to just one page or post or to only pages or posts. For example:

  • You want a featured post plugin to appear only on the front or Home page.
  • Only posts should have a particular message following them, and only when the full post is displayed.
  • You need to execute some CSS code on a particular page to make the headlines green.

All these examples call for the use of what are called “Conditional Tags.” As the WordPress Codex says of them:

Conditional Tags can be used … to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches. For example, you might want to display a snippet of text above the series of posts, but only on the main page of your blog.

Conditional Tags are covered fully on a page in the WordPress Codex, which lists Tags for everything from the Home page to author pages.

This article shows you how to use the most common Tags in situations that many people want to use them in.

Basic use of Conditional Tags

Conditional Tags are always used in functions in the custom_functions.php file, and always within if statements.

All of the Conditional Tags covered in this article have the same syntax:

  • The Tag is followed by opening and closing parentheses.
  • If the Tag applies to all pages or posts there is nothing within the parentheses.
  • The condition to be tested is enclosed in quotes within the parentheses if a particular page or post is being called for. Note that the tags testing or the Home or front pages never have parameters.
  • All Conditional Tags evaluate to either “True” or “False.”

For example, the Conditional Tag testing for a page named “Some Page” would look like this: is_page('Some Page'). If it were simply testing that the document is a page rather than a post, it would be written: is_page()

The most commonly used Conditional Tags

The WordPress Codex article has Tags for everything you can imagine, but I find myself using the same ones over and over. They are:

  • is_home() tests whether the document being displayed is the Home page of a blog.
  • is_front_page() tests for the front page of either a blog or a static site, as set on the “Settings > Reading” admin panel. I tend to use this much more than the is_home() Tag since it covers both blogs and static sites.
  • is_page() determines whether the document on the screen is a page or a post. When used with a parameter, it can test for specific pages.
  • is_single() is used when you want to tell whether the document being displayed is a single post. A parameter within the parentheses enables it to test for a particular post.

Tags for the front or Home page

There are many instances when you only want something to appear on the front or Home page of your site. Using the is_front_page() Conditional Tag allows you to do this. As mentioned, I use this Tag in preference to the is_home() Tag, since is_home() tests only for the Home page of a blog, and is_front_page() tests for the front page of either a blog or a static site. I’m lazy!

Note that is_front_page() and is_home() do not take any parameters. Their opening and closing parentheses are always empty!

Say you are using a featured post plugin that you only want displayed on the front page. Following is how you would do it:

function test_for_front_page() {
if (is_front_page()) {
...code for the plugin...
}
}

You can also use this model for anything that you only want to appear on the front page, such as boxes placed above the sidebars, and even swapping headers (more on putting CSS into a function is found below).

The is_single() Tag for posts

The Tag is_single() is used for posts when they are displayed on a single page, as when you click on a blog post’s title on the front page of your blog and it goes to the post’s page.

Instances for the use of this Tag would be:

  • When you want a special message to be displayed at the end of all posts when displayed on their own page.
  • If you want a particular ad to be displayed only on post pages
  • A warning box should be displayed only on one post

Note: Although there is an is_page() Tag for pages (described below), there is no is_post() Tag! Always use is_single() for posts.

Testing for all posts

If you want all posts (and not pages) to have a particular item, use the is_single() Tag without any parameters within the parentheses. The following example would execute its code for all posts:

function test_for_posts() {
if (is_single()) {
...code for all posts...
}
}

Testing for a single post

To test for a certain post, you must put parameters within the parentheses between single quotation marks. Most of the time, I use either the post name or the post’s number (there are other parameters — check the WordPress Codex article for more).

I usually use the post name in the parameter and only use the post number when some character or word in the title causes PHP to complain. (If you don’t know how to find the post number, check out the “Quickie: How to Find a Page or Post’s Number” article.) For example:

function test_for_post() {
if (is_single('Some Post')) {
...code only for this post...
}
}

or if using the post number…

function test_for_post() {
if (is_single('234')) {
...code only for this post...
}
}

Note that when using post names, the name must be exact, with all the capitals, punctuation, etc!

If you want to be absolutely sure that a post is found, you can test for both the name and number by using the PHP “and” logical operator &&. It returns “True” if both the name of the post is “Some Post” and the post number is “234:”

function test_for_post() {
if (is_single('Some Post') && is_single('234')) {
...code only for this post...
}
}

You can also use the array parameter to test for a single post. The following example returns “True” if either the post name is “Some Post” or the post number is “234.” Note that the array parameter has parentheses of its own, and the post number is not put within single quotation marks:

function test_for_post() {
if (is_single(array(234,'Some Post'))) {
...code only for this post...
}
}

When using the array parameter be careful about your parentheses. In the example above you have one pair of parentheses for the if statement, one for the is_single() Tag and another for the array parameter, which means you end up with three closing parentheses at the end of the statement.

Testing for multiple posts

You can also use these methods to test for multiple posts. Say you want to want to display an ad on either of two posts. You would use the PHP logical operator || which means “or.” The following is_single() Tag would evaluate “true” if the post is either named “First Post” or is number “789:”

function test_for_post() {
if (is_single('First Post') || is_single('789')) {
...code for either post...
}
}

Once again, you can use the array parameter, this time for multiple posts. In the following example, we are testing for posts named “First Post,” “Second Post,” and “Third Post.” If the post being tested has any of these names it returns “True.” Note that the post names are enclosed within single quotation marks, and have a comma but no spaces between them:

function test_for_post() {
if (is_single(array('First Post','Second Post','Third Post'))) {
...code for any of these posts...
}
}

The is_page() Tag for pages

The is_page() Tag is used for pages (not posts). Its parameter syntax and uses are the same as for posts, so they will only be mentioned here:

  • Use the is_page() Tag without anything within the parentheses to test for all pages.
  • To test for a single page, you can use the page name within the Tag’s parameters, for example: is_page('Some Page')
  • You can also use the page number to test for a single page: is_page('234')
  • The syntax is the same for testing for multiple posts as for pages, except you use the is_page() Tag instead of the is_single() tag.

This example tests that the document is a page and not something else:

function test_for_any_page() {
if (is_page()) {
...code for all pages...
}
}

The following example tests for a page named “Some Page” and with the page number “234:”

function test_for_page() {
if (is_page('Some Page') && is_single('234')) {
...code only for this page...
}
}

And this example will evaluate as true if any of the pages have one of the names listed:

function test_for_pages() {
if (is_page(array('First Page','Second Page','Third Page'))) {
...code for any of these pages...
}
}

Executing CSS on pages or posts

In the article “Quickie: Executing CSS in custom_functions.php” I show you how to execute CSS within the PHP of your custom_functions.php file. Here is how to do it for just one post using one of the examples above — I’m sure you can figure out how to use it in the other instances covered above:

function test_for_post() {
if (is_single('Some Post')) {
?>
<style type="text/css">
.custom #some_id { background: #000000; }
.custom .some_class { position: relative; }
</style>
<?php
}
}

Conclusion

I hope this article is useful to you and that it will enable you to do things in custom_functions.php that you weren’t able to do before. As always, your comments are welcome. If you want to contact me directly by email, you may click on the “Contact” tab in the menu.

©2009 Michael L Nichols. All rights reserved.

What next?

Your comments are always welcome, and are important to this blog’s community! Leave a comment now, or read the comments.

You can find several related articles in the “Related Articles” list below. In the footer you will find a lists of Popular Posts, Recent Posts, and you may browse by Categories, or tags. There’s also a Google Custom Search box to help you find just what you want.

Get free updates by RSS or email!

If you have enjoyed this article, please consider subscribing to article updates, using an RSS reader, or by email. It’s free and is a great way to make sure you don’t miss a single article! I also invite you to follow me on Twitter!

Why not share this article with others!

Share this article with your friends using your favorite social media service, such as StumbleUpon, or Digg. Check out the icons below under “Share This Article With Others” for other social media, including del.icio.us, Technorati, Sphinn, Friendfeed, FaceBook, MySpace andLinkedIn! You can also email or print the article, and even tweet it using Twitter!

New Tutorial! Tabbed Info Pane

You may have noticed a new addition to this blog page. At the top is a tabbed box containing recent posts, popular articles, and recent comments widgets, as well as a coming soon tab.

This addition is the Tabbed Info Pane, and I have just written a tutorial to show you how to use it on your site.

You will find the Tabbed Info Pane very versatile and easy to use. It can be used anywhere on the page where there is a Thesis hook, and the basic idea can even be used in widgets.

The Tabbed Info Pane can hold almost any kind of information you can put into HTML or PHP. For example:

  • Text
  • Lists
  • Tables
  • Images
  • PHP functions
  • Widgets

A few of the features of the Tabbed Info Pane are:

  • Easy to set up and use.
  • It is very flexible and can be used in many different ways.
  • Requires no plugins. It is written entirely in JavaScript and CSS.
  • Coexists with JQuery sliders, menus, featured posts plugins and other JavaScript elements
  • Its simple programming ensures trouble-free operation.
  • Can contain almost any kind of information or element
  • Highly configurable. You can change almost everything about the appearance of the Info Pane.
  • SEO friendly. The information is contained in the HTML of your page so Google’s and other bots can index it.

I hope you give the Tabbed Info Pane a try on your site. You will find it a great enhancement that takes only minutes to set up.