Using conditional tags to display content on specific pages

Sometimes, you may want to display some content only on specific pages or sections. For example, you may wish to display a welcome message on your blog homepage or show specific information on the Categories page. In this recipe, Let's learn how to do it with the help of WordPress conditional tags.

Getting ready

To achieve this recipe, you'll need a WordPress theme that you can edit.

How to do it

1. WordPress conditional tags are Boolean variables so you have to use them as a condition on a php if statement as shown in the following example:

echo "Page title:"; the_title;

2. The preceding if statement will return false if the current page template isn't a WordPress page template so nothing would happen. Otherwise, the if statement will return true and will print the page title on screen.

3. Conditional tags can be used in any theme files. Available conditional tags

WordPress features a large amount of conditional tags for all needs. They all work as described above. Following are the conditional tags:

► is_home(): It returns true if the current page is the blog homepage

► is_front_page() : It returns true if the current page is the blog front page

► is_single() : It returns true if the current page is a single post template

► is_page(): It returns true if the current page is a page template

► is_page_template("about.php"): It returns true if a page template is currently being used

► is_category(): It returns true if the current page is a category template

► in_category('4'): It returns true if the current posts belongs to the specified category

► is_tag(): It returns true if the current page is a tag template

► has_tag("wordpress"): It returns true if the post have the tag specified in parameter

► is_author(): It returns true if the current page is an author archive

► is_date(): It returns true if the post or page is a date-based archive

► is_year(): It returns true if it's a yearly archive

► is_month(): It returns true if it's a monthly archive

► is_day(): It returns true if it's a daily archive

► is_time(): It returns true if an hourly, minutely, or secondly archive is being displayed

► is_archive(): It returns true if the current page display any type of archives (time, author, tag, and so on)

► is_search(): It returns true if the current page displays search results

► is_paged(): It returns true if the current page is paged

► is_4 04(): It returns true if the current page is a 404

► is_sticky(): It returns true if the Stick this post to the front page check box has been checked for the current post

► has_tag("WordPress"): It return true if the current posted has been tagged with WordPress

► is_admin(): It returns true if the dashboard or an admin page is currently displayed

► comments_open(): It returns true if commenting is allowed on the post

► pings_open(): It returns true if pinging is allowed on the post

► is_preview(): It returns true if the post or page is displayed in preview mode

The is_home() function has been deprecated and replaced by the is_front_page() function. To ensure backward compatibility with older WordPress versions, you should use the following piece of code:

How it works?

Unless specified, the above tags doesn't take any parameter. They have to be used in a PHP conditional statement.

//Do something only if the post or page is a 4 04 error.

On the other hand, some conditional tags request a parameter to work. For example, to use the is_year() tag, you have to provide a year in parameter.

//Do something only if the post or page was published during the year 2007.

Conditional tags, as we have seen, are very useful. But there's more—some conditional tags accept optional arguments that allow you to create a more specific condition. For example, you should create a code snippet that will only be executed if the current page is a WordPress template.

Special parameters

Some conditional tags can accept up to 4 different types of parameters:

► ID: The ID parameter can be used in is_category, is_page(), is_tag(), is_ single(), is_author(), and is_sticky() .

As a result, the preceding code returns true if the category ID is 5.

► Name: The name parameter can be used in is_category, is_page(), is_single() and is_author().

<?php if (is_category("Blogging Tips")) {

As a result, the preceding code returns true if the category name is

"Blogging Tips"

► Slug: The slug parameter can be used in is_category, is_page(), is_tag(), is_single(), and is_author().

<?php if (is_category("blogging-tips")) {

As a result, the preceding code returns true if the category slug is "blogging-tips".

► Array: The array parameter can be used in is_category, is_page(), is_tag(), has_tag(), is_single() and is_author().

<?php if(is_category(array(5,,blogging-tips,,,Blogging Tips'))) {

As a result, the preceding code returns true if the category of posts being displayed either has the ID as 5, slug as "blogging-tips", or name as "Blogging Tips".

Make Money Blogging

Make Money Blogging

It is fine to get started on a hosted solution like Blogger.comor WordPress.com. They will allow you to learn the basics of blogging and web publishing without spending money. As soon as you nail down these basics, however, it is vital to buy your own domain name and to purchase a web hosting package with some company.

Get My Free Ebook


Post a comment