Codex tools: Log in / create account
The Conditional Tags can be used in your Template files 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. With the is_home() Conditional Tag, that task is made easy.
Note the close relation these tags have to WordPress' Template Hierarchy.
All of the Conditional Tags test to see whether a certain condition is met, and then returns either TRUE or FALSE. The conditions under which various tags output TRUE is listed below. Those tags which can accept parameters are so noted.
This section refers to WordPress Pages, not any generic webpage from your blog.
There is no is_subpage() function yet, but you can test this with a little code:
<?php
// Get $post if you're inside a function
global $post;
if (is_page() && $post->post_parent ) {
// This is a subpage
} else {
// This is not a subpage
}
?>
If you need to test whether this is a particular page OR a child of that page (e.g. to present a different banner on different sections of a page-based site), get the parent page IDs from the back-end, then use code like this:
<?php
if (is_page(about) || $post->post_parent=="2") {
$bannerimg="home.jpg";
} elseif (is_page(learning) || $post->post_parent=="56") {
$bannerimg="teaching.jpg";
} elseif (is_page(admissions) || $post->post_parent=="15") {
$bannerimg="admissions.jpg";
} else {
$bannerimg="home.jpg" ; // Fall-through
}
?>
Beginning with Version 2.5 this allows you to determine whether or not you are in a page template or if a specific page template is being used.
Note: Be sure to check your spelling when testing, "is" and "in" are a big difference.
See also is_archive() and Category Templates.
See also is_archive() and Tag Templates.
See also is_archive() and Author Templates.
See also is_archive().
Here are working samples to demonstrate how to use these conditional tags.
This example shows how to use is_single() to display something specific only when viewing a single post page:
if (is_single())
{
echo 'This is just one of many fabulous entries in the ' . single_cat_title() . ' category!';
}
If someone browses our site by date, let's distinguish posts in different years by using different colors:
<?php
// this starts The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<?php
// are we showing a date-based archive?
if (is_date())
{
if (date('Y') != get_the_date('Y'))
{
// this post was written in a previous year
// so let's style the content using the "oldentry" class
echo '<div class="oldentry">';
} else {
echo '<div class="entry">';
}
} else {
echo '<div class="entry">';
}
the_content('Read the rest of this entry »');
?>
</div>
This example will display different content in your sidebar based on what page the reader is currently viewing.
<!-- begin sidebar -->
<div id="sidebar">
<?php
// let's generate info appropriate to the page being displayed
if (is_home()) {
// we're on the home page, so let's show a list of all top-level categories
echo "<ul>";
wp_list_cats('optionall=0&sort_column=name&list=1&children=0');
echo "</ul>";
} elseif (is_category()) {
// we're looking at a single category view, so let's show _all_ the categories
echo "<ul>";
wp_list_cats('optionall=1&sort_column=name&list=1&children=1&hierarchical=1');
echo "</ul>";
} elseif (is_single()) {
// we're looking at a single page, so let's not show anything in the sidebar
} elseif (is_page()) {
// we're looking at a static page. Which one?
if (is_page('About')) {
// our about page.
echo "<p>This is my about page!</p>";
} elseif (is_page('Colophon')) {
echo "<p>This is my colophon page, running on WordPress " . bloginfo('version') . "</p>";
} else {
// catch-all for other pages
echo "<p>Vote for Pedro!</p>";
}
} else {
// catch-all for everything else (archives, searches, 404s, etc)
echo "<p>Pedro offers you his protection.</p>";
} // That's all, folks!
?>
<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<input type="text" name="s" id="s" size="15" />
<input type="submit" value="<?php _e('Search'); ?>" />
</div>
</form>
</div>
<!-- end sidebar -->
When a visitor gets a 404 error page, it can be intimidating, and unhelpful. Using Wordpress, you can take the edge off a 404 and make it helpful to users, and yourself, too, by emailing whenever the user clicks a link to a non-existent page. If you use this, don't forget o
<p>You
<?php
#some variables for the script to use
#if you have some reason to change these, do. but wordpress can handle it
$adminemail = get_bloginfo('admin_email'); #the administrator email address, according to wordpress
$website = get_bloginfo('url'); #gets your blog's url from wordpress
$websitename = get_bloginfo('name'); #sets the blog's name, according to wordpress
if (!isset($_SERVER['HTTP_REFERER'])) {
#politely blames the user for all the problems they caused
echo "tried going to "; #starts assembling an output paragraph
$casemessage = "All is not lost!";
} elseif (isset($_SERVER['HTTP_REFERER'])) {
#this will help the user find what they want, and email me of a bad link
echo "clicked a link to"; #now the message says You clicked a link to...
#setup a message to be sent to me
$failuremess = "A user tried to go to $website"
.$_SERVER['REQUEST_URI']." and received a 404 (page not found) error. ";
$failuremess .= "It wasn't their fault, so try fixing it.
They came from ".$_SERVER['HTTP_REFERER'];
mail($adminemail, "Bad Link To ".$_SERVER['REQUEST_URI'],
$failuremess, "From: $websitename <noreply@$website>"); #email you about problem
$casemessage = "An administrator has been emailed
about this problem, too.";#set a friendly message
}
echo " ".$website.$_SERVER['REQUEST_URI']; ?>
and it doesn't exist. <?php echo $casemessage; ?> You can click back
and try again or search for what you're looking for:
<?php include(TEMPLATEPATH . "/searchform.php"); ?>
</p>
The Dynamic Menu Highlighting article discusses how to use the conditional tags to enable highlighting of the current page in a menu.