Tag Archives: custom_functions.php

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

Another Tip! Quickie: Executing CSS in custom_functions.php

Sometimes we need to execute CSS within the PHP of our custom_functions.php file. This article shows you how to use the “style” statement to do so!

Quickie: Executing CSS in custom_functions.php” is a clear and concise article on how to use the same CSS statements as you use in the custom.css file within your custom_functions.php file. It has an example that illustrates the method.

Yet another “Quickie” filed under the “Tips” section in the “Tips” grouping.

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!

Quickie: Executing CSS in custom_functions.php

Sometimes we need to execute CSS code within the PHP in our custom_functions.php file. It’s a simple trick! You use the style statement to wrap your CSS in.

Here’s an example for you:

function use_style_statement() {
?>
<style type="text/css">
.custom #some_id { background: #000000; }
.custom .some_class { position: relative; }
</style>
<?php
}

Note these things:

  • You must turn PHP off with the ?> statement before using the style statement and back on with <?php when you are finished.
  • The CSS statements within the style statement are the same as you would use in the custom.css file

Now, isn’t that easy?

I hope you are able to use this quickie in your code! As always, feel free to leave a comment below. If you want to contact me directly by email, just click 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 Tip! Upgrade-Proof Your Site with the bloginfo() Tag

Thesis has the reputation of being upgrade-proof. That is, you do not lose your work in the custom folder when you upgrade.

Running against this is the necessity of putting full URL paths to images and files in the custom_functions.php file. If you use a lot of images from your /custom/images folder, it can be a real pain going through the file updating the URLs.

But you really don’t have to do that — not if you use the WordPress bloginfo() tag!

The article Upgrade-Proof Your Site with the bloginfo() Tag in the “Tips” section shows you how to use the tag to automatically put your Thesis path into URLs. That means that no matter what your Thesis folder is named, your URLs will always be up-to-date!

If you do a lot of work in the custom_functions.php file, this article is for you!

New Articles for Newbies: Basic Syntax for custom.css and custom_functions.php!

People who are just getting their feet wet with customizations are often daunted by the strange words and punctuation they find in the code for custom.css and custom_functions.php. Even the basic format of the CSS and PHP languages are foreign.

Two new articles just published in the Newbies “Getting Started” section, “Basic CSS Syntax for custom.css” and “Basic PHP Syntax for custom_functions.php” provide just the kind of information newbies need to get a grip on their first adventures with CSS and PHP in Thesis.

Both articles are filled with illustrations of what is being discussed, and are written in clear and concise language. They cover just the what’s needed to work with Thesis and leave the long tutorials to someone else.

The PHP article, in particular, is full of advice for preventing syntax mistakes and troubleshooting errors in the custom_functions.php file. This is because even the smallest syntax error in the PHP code will bring your blog crashing down, preventing access to your blog site, and delivering only a cryptic error message. In a large number of cases — perhaps the majority — the problem is not bad code, but some tiny syntax error.

I have many more articles on tap for Newbies, and am receiving some good suggestions for more. If you would like to see an article on a subject that is puzzling you, please leave a comment or email me directly by clicking the “Contact” button.

Basic PHP Syntax for custom_functions.php

The custom_functions.php file is used less often for customization in Thesis than the custom.css file, but it is viewed with fear all out of proportion to how much it is employed. That’s because PHP errors in the custom_functions.php file can stop your site cold with a cryptic error message, whereas an error in custom.css will just make the selector do nothing.

The parts of the PHP language used by Thesis is limited, so there’s not so much to learn as with the CSS in the custom.css file. And the requirements and syntax used by PHP are simpler than CSS, but they are very, very strict and unforgiving. In a large portion of cases, a PHP error is caused by a simple syntax mistake rather than genuinely bad code.

This guide shows you the basics of PHP syntax as it applies to Thesis and the custom_functions.php file, and points out the most common mistakes people make. It is intended to help you prevent mistakes as well as to help you track down and correct PHP syntax errors that can cause your site to come to a screeching halt. It is not intended to be a complete tutorial, which would take many pages. A good online PHP tutorial is available on the w3schools.com site. The tutorial is useful not only for learning what you need to know about PHP, but as a reference as well.

The basic PHP scripting block

A block of PHP code can be written in as short a unit as a part of one line, or it can be many lines long. Whatever its size, it always requires a “<?php” to open PHP and make it active and “?>” to close it. Here are examples of PHP code blocks:

<?php SOME PHP CODE HERE ?>

<?php
	SOME PHP CODE HERE
	SOME MORE PHP CODE HERE
?>

You will sometimes see PHP code with a “<?” at the beginning of the code block instead of “<?php”. This is called shorthand, and is not available on all servers. For maximum compatibility, you should use the standard form “<?php” rather than the shorthand.

PHP functions and PHP statements in custom_functions.php require PHP to be open to work

Since Thesis makes limited use of the PHP language, almost all of the customizations you will ever do in custom_functions.php will involve PHP functions or PHP statements. These both require that PHP be open and active for them to work. You do this by making sure that the function or statement is preceded by a “<?php” in the code above it. Note that the custom_functions.php file actually begins with a “<?php”.

One of the most common errors in custom_functions.php is for PHP not to be active when required. But don’t riddle your code with “<?php” everywhere — that will produce errors, too. All it takes is one “<?php” to make PHP active. A good practice is to exit every function with PHP open.

If you have an error, the first thing to look for is that there is a “<?php” activating PHP somewhere above the new PHP code you have written.

A typical Thesis PHP statement

Probably the most common PHP statements in custom_functions.php are detaching or attaching a function to a Thesis hook. The following example has statements that move the Thesis navigation menu from above the header to below it.

The statements in the example have two important PHP syntax characteristics. Each PHP statement ends with a semicolon (;). This tells PHP to process that line. If you leave the semicolon out, either you will get an error message or nothing will happen. Note also that PHP typically uses single quotation marks (‘), unlike HTML which uses double quotation marks (“). Mixed up or unbalanced single and double quotation marks are frequent causes of custom_functions.php errors.

Remember that PHP statements require a “<?php” in the code above it to work and not cause errors:

<?php
remove_action('thesis_hook_before_header', 'thesis_nav_menu');
add_action('thesis_hook_after_header', 'thesis_nav_menu');

The basic PHP function

You sometimes will be cutting-and-pasting functions or building your own functions in custom_functions.php. Functions all have the same basic format and syntax. Remember that they require a “<?php” in the code above it to be recognized by PHP and not cause an error message.

Functions are actually quite simple. They begin with the word “function,” which is followed by the function name, then an opening and closing parenthesis “()” and finally an opening curly bracket “{“. After its contents, the function is then closed by a closing curly bracket “}”. For example:

<?php
function FUNCTION-NAME() {
	CONTENTS OF FUNCTION
}

A common error is to forget to close the function with a closing curly bracket “}”. It may not even cause an error message, but the function definitely will not work.

PHP comments in custom_functions.php

It is good coding practice to label the parts of your code with comments. Besides helping you remember what a block of code does after time has passed, it helps others read your code more easily. Comment marks are also useful to temporarily disable a block of code.

There are two ways to put comments into PHP: one method for single lines, and another for multiple lines. Single-line comments are preceded by two right slashes “//”. Multiple-line comments are preceded by “/*” and ended with “*/”. Either of these kinds of comments can be used for single lines and may be placed at the end of a line of code.

I always use the multiple-line comment style for everything for the simple reason that it’s also the commenting syntax required for the CSS in the custom.css file: One less thing to remember, one fewer source of errors!

Following is an illustration of both styles of comments, along with a block of PHP code that has been commented out by enclosing it within a “/*” and a “*/”:

// This is a single line comment

/* This is a multiple line comment
continued onto a second line. */

/* The multiple line comment syntax can be used for everything. */

/* A block of code commented out */
/*function bad_code() {
	ERROR-PRODUCING CODE
}*/

I hope this short guide will help you format your PHP well, and track down errors when your custom_functions.php file gives you trouble. Remember that your comments are always welcome, and that you can email me directly by clicking on the “Contact” button at the top of the page.

©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!