Codex tools: Log in / create account
Languages: English • Français • 日本語 • Türkçe • 中文(简体) • (Add your language)
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.
Note: If you select a static Page as your frontpage (see below), this tag will be applied to your "posts page".
Note: The array ability was added at Version 2.5. This function does not distinguish between the post ID, post title, or post name. A post named "17" would be displayed if a post ID of 17 was requested.
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:
Snippet 1
<?php
global $post; // if outside the loop
if ( is_page() && $post->post_parent ) {
// This is a subpage
} else {
// This is not a subpage
}
?>
You can create your own is_subpage() function using the code in Snippet 2. Add it to your functions.php file. It tests for a parent page in the same way as Snippet 1, but will return the ID of the page parent if there is one, or false if there isn't.
Snippet 2
function is_subpage() {
global $post; // load details about this page
if ( is_page() && $post->post_parent ) { // test to see if the page has a parent
$parentID = $post->post_parent; // the ID of the parent is this
return $parentID; // return the ID
} else { // there is no parent so...
return false; // ...the answer to the question is false
};
};
It is advisable to use a function like that in Snippet 2, rather than using the simple test like Snippet 1, if you plan to test for sub pages frequently.
To test if the parent of a page is a specific page, for instance "About" (page id pid 2 by default), we can use the tests in Snippet 3. These tests check to see if we are looking at the page in question, as well as if we are looking at any child pages. This is useful for setting variables specific to different sections of a web site, so a different banner image, or a different heading.
Snippet 3
<?php
if ( is_page('about') || $post->post_parent == '2' ) {
// the page is "About", or the parent of the page is "About"
$bannerimg = 'about.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'; // just in case we are at an unclassified page, perhaps the home page
}
?>
Snippet 4 is a function that allows you to carry out the tests above more easily. This function will return true if we are looking at the page in question (so "About") or one of its sub pages (so a page with a parent with ID "2").
Snippet 4
function is_tree($pid) { // $pid = The ID of the page we're looking for pages underneath
global $post; // load details about this page
if(is_page()&&($post->post_parent==$pid||is_page($pid)))
return true; // we're at the page or at a sub page
else
return false; // we're elsewhere
};
Add Snippet 4 to your functions.php file, and call is_tree('id') to see if the current page is the page, or is a sub page of the page. In Snippet 3, is_tree('2') would replace "is_page('about') || $post->post_parent == '2'" inside the first if tag.
Note that if you have more than one level of pages the parent page is the one directly above and not the one at the very top of the hierarchy.
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().
See also is_archive() and Author Templates.
See also is_archive().
<?php
// Get $post if you're inside a function
global $post;
if ( empty($post->post_excerpt) ) {
// This post has no excerpt
} else {
// This post has excerpt
}
?>
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!';
}
Other example how to use Conditional Tags into loop. choose display content or excerpt in the index.php, when this is unique archive for display single post, categories, home and page.
if (is_home() || is_single()) {
the_content();
}
else {
the_excerpt();
}
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 -->
The Creating an Error 404 Page article has an example of using the PHP conditional function
isset()
in the Writing Friendly Messages section.
The Dynamic Menu Highlighting article discusses how to use the conditional tags to enable highlighting of the current page in a menu.
At times queries performed in other templates such as sidebar.php may corrupt certain conditional tags. For instance, in header.php a conditional tag works properly but doesn't work in a theme's footer.php. The trick is to put wp_reset_query before the conditional test in the footer. For example:
<?php
wp_reset_query();
if (is_page('2') ) {
echo 'this is page 2!';
}
?>