HowTo – WordPress Conditional Tags To Control When Things Happen

Filed under: Wordpress Tips

wordpress_logoWordPress has a number of ways to format your themes including all of the different template files you can use to limit what happens when your visitors view a Page, Post, Category Listing, Archive Listing, or your Home page.

In addition to the Theme Template files you can also use Conditional Tags to instruct WordPress to do things when people are visiting certain Pages, Posts or when they are reading a post inside a certain Category or if they are viewing the Home Page.

When is this useful?

Well the real uses are limitless but in reality one of the most useful things about conditional tags is formatting your menus and sidebars when your visitors are reading specific content.

For instance if you had a conditional tag set for your Contact Page then you could remove advertising just for that page by loading or unloading sidebars limiting functions.

You can also setup added content that will only be displayed if you are an Administrator.

For example if you wanted something to show on all of your single post pages you could place this in your sidebar.php

if (is_single())
{
     echo 'This is a single post menu';
}

Obviously you would put a whole menu or banner or something useful in your code.

However if you only wanted to change something when a specific post is being shown you might use.

is_single(’35’)

Where 35 is the ID of the post or

is_single(‘Map Post’)
Where
the title of the page is Map Post

is_single(‘map-post’)
Where the slug of the page is map-post

or maybe you have a category full of posts with maps.

in_category(‘5’)
would work when ever a post is shown and it is in category 5

.

There are a heck of a lot of Conditional variables that you can use to limit or display information. And you should try to be as creative as you can when using conditional tags.

For more information visit the WordPress Codex page.

http://codex.wordpress.org/Conditional_Tags

.

Here is a list of Conditions that you can use in your themes to restrict or display content based on if the value is true or not.

comments_open
has_tag
in_category
is_404
is_admin
is_archive
is_attachment
is_author
is_category
is_comments_popup
is_date
is_day
is_feed
is_front_page
is_home
is_month
is_page
is_page_template
is_paged
is_preview
is_search
is_single
is_singular
is_sticky
is_tag
is_time
is_trackback
is_year
pings_open