跳转至: 导航搜索

2013年8月2日 (五) 04:34的最新版本




注: the close relation these tags have to WordPress Template Hierarchy.

The Conditions For ...


主页(The Main Page)


注: 如果你使用一个静态页面作为首页(front page,见下文),该标签将应用到文章页面(posts page).

首页(The Front Page)

当首页显示时,不管首页是文章还是一个页面. 当显示

博客首页并且'设置 > 阅读 ->首页显示' 设置为 "最新文章", '设置 > 阅读 ->首页显示' 设置为 "静态页面" 同时"静态页面"值为当前显示的页面时.返回true

注: 该标签在Version 2.5中增加.




is_single('Irish Stew') 
当标题为"Irish Stew"的文章单独显示时.
is_single(array(17,'beef-stew','Irish Stew')) 
当文章id为17,或者别名为"beef-stew",或者标题为"Irish Stew" 都返回Ture。 注:该数组在2.5版本中添加。


Returns true if "Stick this post to the front page" check box has been checked for the current post. In this example, no post ID argument is given, so the post ID for the Loop post is used. Note: this tag was added at Version 2.7.


When in Comments Popup window.


When comments are allowed for the current Post being processed in the WordPress Loop.
When pings are allowed for the current Post being processed in the WordPress Loop.


This section refers to WordPress Pages, not any generic webpage from your blog.

When any Page is being displayed.
When Page 42 (ID) is being displayed.
is_page('About Me And Joe') 
When the Page with a post_title of "About Me And Joe" is being displayed.
When the Page with a post_name (slug) of "about-me" is being displayed.
is_page(array(42,'about-me','About Me And Joe')) 
Returns true when the Pages displayed is either post ID 42, or post_name "about-me", or post_title "About Me And Joe". Note: the array ability was added at Version 2.5.


There is no is_subpage() function yet, but you can test this with a little code:

// 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

You could add this function to your templates functions.php file:

function is_tree($pid) {    // $pid = The page we're looking for pages underneath
	global $post;       // We load this as we're outside of the post
	if(is_page()&&($post->post_parent==$pid||is_page($pid))) return true; // Yes, it's in the tree
	else return false;  // No, it's outside

and call is_tree('id') to see if the page is in the tree. In the example below 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 top of the tree.

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:


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  


If you wish to test this numerous times it is advisable to create a function (is_cpage()) which can easily be maintained. For example, if is_subpage() is introduced in to wp, you can change your function in functions.php without having to change multiple uses.


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.

Is a Page Template being used?
Is Page Template 'about' being used? Note that unlike with other conditionals, if you want to specify a particular Page Template, you need to use the filename, such as about.php or my_page_template.php.


When any Category archive page is being displayed.
When the archive page for Category 9 is being displayed.
is_category('Stinky Cheeses') 
When the archive page for the Category with Name "Stinky Cheeses" is being displayed.
When the archive page for the Category with Category Slug "blue-cheese" is being displayed.
is_category(array(9,'blue-cheese','Stinky Cheeses')) 
Returns true when the category of posts being displayed is either term_ID 9, or slug "blue-cheese", or name "Stinky Cheeses". Note: the array ability was added at Version 2.5.
Returns true if the current post is in the specified category id. read more

Note: Be sure to check your spelling when testing, "is" and "in" are a big difference.

See also is_archive() and Category Templates.


When any Tag archive page is being displayed.
When the archive page for tag with the slug of 'mild' is being displayed.
Returns true when the tag archive being displayed has a slug of either "sharp", "mild", or "extreme". Note: the array ability was added at Version 2.5.
When the current post has a tag. Must be used inside The Loop. Note: has_tag was added at Version 2.6.
When the current post has the tag 'mild'.
When the current post has any of the tags in the array.

See also is_archive() and Tag Templates.


When any Author page is being displayed.
When the archive page for Author number (ID) 4 is being displayed.
When the archive page for the Author with Nickname "Vivian" is being displayed.
When the archive page for the Author with Nicename "john-jones" is being displayed.
When the archive page for the author is either user ID 4, or user_nicename "john-jones", or nickname "Vivian". Note: the array ability was added at Version 2.5.

See also is_archive() and Author Templates.


When any date-based archive page is being displayed (i.e. a monthly, yearly, daily or time-based archive).
When a yearly archive is being displayed.
When a monthly archive is being displayed.
When a daily archive is being displayed.
When an hourly, "minutely", or "secondly" archive is being displayed.

See also is_archive().


When any type of Archive page is being displayed. Category, Tag, Author and Date based pages are all types of Archives.


When a search result page archive is being displayed.


When a page displays after an "HTTP 404: Not Found" error occurs.

A Paged Page

When the page being displayed is "paged". This refers to an archive or the main page being split up over several pages. This does not refer to a Post or Page whose content has been divided into pages using the <!--nextpage--> QuickTag.

An Attachment

When an attachment document to a post or Page is being displayed. An attachment is an image or other file uploaded through the post editor's upload utility. Attachments can be displayed on their own 'page' or template. For more information, see Using Image and File Attachments.

A Single Page, Single Post or Attachment

When any of the following return true: is_single(), is_page() or is_attachment().

A Syndication

When the site requested is a Syndication. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers.

A Trackback

When the site requested is WordPress' hook into its Trackback engine. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers.

A Preview

When a single post being displayed is viewed in Draft mode.

Has An Excerpt

When the current post has an excerpt
When the post 42 (ID) has an excerpt.
// 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

Inside The Loop

Check to see if you are "inside the loop". Useful for plugin authors, this conditional returns as true when you are inside the loop.

Is Sidebar Active

Check to see if a given sidebar is active (in use). Returns true if the sidebar (identified by name, id, or number) is in use, otherwise the function returns false. This conditional function became available with Version 2.8.

Working Examples

Here are working samples to demonstrate how to use these conditional tags.

Single Post

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!';

Date-Based Differences

If someone browses our site by date, let's distinguish posts in different years by using different colors:

// 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>

// 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 »'); 

Variable Sidebar Content

This example will display different content in your sidebar based on what page the reader is currently viewing.

<!-- begin sidebar -->
<div id="sidebar">
// 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>";
        echo "</ul>";
} elseif (is_category()) {
        // we're looking at a single category view, so let's show _all_ the categories
         echo "<ul>";
        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']; ?>">
<input type="text" name="s" id="s" size="15" />
<input type="submit" value="<?php _e('Search'); ?>" />

<!-- end sidebar -->

Helpful 404 Page

The Creating an Error 404 Page article has an example of using the PHP conditional function %%%isset()%%% in the Writing Friendly Messages section.

Dynamic Menu Highlighting

The Dynamic Menu Highlighting article discusses how to use the conditional tags to enable highlighting of the current page in a menu.

In a theme's footer.php file

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:

if (is_page('2') ) {
echo 'this is page 2!';

Conditional Tags Index

Aphabetical List

Function Reference

Conditional Tags: comments_open(), is_local_attachment(), is plugin active(), 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_new_day(), is_page(), is_page_template(), is_paged(), is_plugin_page(), is_preview(), is_search(), is_single(), is_singular(), is_sticky(), is_tag(), is_tax(), is_taxonomy_hierarchical(), is_time(), is_trackback(), is_year(), in_category(), in_the_loop(), is_active_sidebar(), is_active_widget(), is_blog_installed(), is_dynamic_sidebar(), is_user_logged_in(), has_excerpt(), has_post_image(), has_tag(), pings_open(), post_type_exists(), taxonomy_exists(), term_exists(), username exists()

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude


This Template is used by Codex:Template Messages.


pre 检查到模板循环:模板:Message /pre