Understanding Conditional Statements
Basic Code
Here is a normal version of my header (minus the shorcut url and the favicon url):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
<?php bloginfo('name'); ?> | <?php wp_title(''); ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo('charset'); ?>" />
<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" />
<meta name="description" content="<?php bloginfo('description'); ?>" />
<link rel="stylesheet" type="text/css" media="screen" href="<?php bloginfo('stylesheet_url'); ?>" />
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
</head>
In the title tag you can see that there is two WordPress common functions(WordPress tags) with a seperator in between, The bloginfo('name'); Outputs your website name and wp_title(''); outputs the pages title
Note: the seperator can also be created inside the title function:
<?php wp_title('|',true,'left'); ?>
The three pockets inside wp_title("$THE_SEPERATOR ", "$SHOULD_WE_ECHO " , " $THE_SEPERATORS_POSITION") pretty easy right
Conditional Code
Here is a version of my header with a large set of conditional tags for the title tag in the head tag:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>
<?php
if (is_home()) { echo bloginfo('name'); }
elseif (is_404()) { bloginfo('name'); echo ' | Oops, this is a 404 page'; }
elseif ( is_search() ) { bloginfo('name'); echo (' | Search Results');}
else { bloginfo('name'); echo (' | '); wp_title(''); }
?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php bloginfo('charset'); ?>" />
<meta name="generator" content="WordPress <?php bloginfo('version'); ?>" />
<meta name="description" content="<?php bloginfo('description'); ?>" />
<link rel="stylesheet" type="text/css" media="screen" href="<?php bloginfo('stylesheet_url'); ?>" />
<link rel="alternate" type="application/rss+xml" title="<?php bloginfo('name'); ?> RSS Feed" href="<?php bloginfo('rss2_url'); ?>" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
</head>
The first line after the php tags delegate that If is_home() echo just the website name, since the website name and the title on the home page are the same. It then goes into an Else if saying that if is a 404 page(page not found on server) then display the website name, the seperator, and then the custom message of "Oops, this is a 404 page". There is also one for the search results page that displays the website name, the seperator, then the line "Search Results". The last line is an Else statement that says if not any of the other pages then display the website name, the seperator, and the page title.
I hope this is'nt confusing .
Bonus Code
I will give you one more example:
<?php if(is_home()) { ?>
<link rel="stylesheet" type="text/css" media="screen" href="<?php bloginfo('template_url'); ?>/css/home.css" />
<?php } elseif (is_page('contact')) { ?>
<link rel="stylesheet" type="text/css" media="screen" href="<?php bloginfo('template_url'); ?>/css/contact.css" />
<?php } else { ?>
<link rel="stylesheet" type="text/css" media="screen" href="<?php bloginfo('stylesheet_url'); ?>" />
<?php } ?>
In this example i used a conditional statement to switch the stylesheet depending on which page we are on. If you are on the homepage it will use home.css, else if you are on the contact page it will use contact.css, else it will revert back to the defualt css file style.css.
Here is a list of other cool conditional tags you can use:
* 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_tax
* is_time
* is_trackback
* is_year
* pings_open
To learn more about conditional tags check out the Codex at WordPress.org







