Wordpress条件标签的使用

zh-cn:条件标签

概述

条件标签函数可使用在模板文件中,用来决定当页面满足什么条件时显示某些内容。例如,你想在文章列表的上面显示一些文本,并且只想在主页上才这么做,使用is_home()条件标签,可以很容易的做到这点.

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

各种条件逻辑 ...

条件标签会测试某个条件是否满足,并返回相应的TRUE或者FALSE,下面列出在何种条件下标签输出为TRUE。这些标签可以接受参数

主页(The Main Page)

is_home() 
当主页显示时返回True

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

首页(The Front Page)

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

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

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

管理员面板

is_admin()
当控件面板或者管理员面板显示时返回true

单文章页面

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

文章置顶

is_sticky() 
   如果当前文章被置顶,则返回True,在本例中,没有文章ID做参数,所以文章ID在循环文章中使用。注意:这个函数是在 版本2.7 被加入。
is_sticky('17') 
当ID为17的文章被置顶,返回Ture。

弹出式评论

is_comments_popup() 
当是弹出评论窗口时,返回True。

包含日志的页面

comments_open()
   
When comments are allowed for the current Post being processed in the  WordPress Loop.
pings_open()
When pings are allowed for the current Post being processed in the  WordPress Loop.

Page页面

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

is_page() 
当为一个Page页面时,返回True。
is_page('42') 
当一个Page页面为42时,返回True。
is_page('About Me And Joe') 
   当一个Page页面的标题是About Me And Joe时,返回True。
is_page('about-me') 
   当一个Page页面的别名(slug)是about-me时,返回True。
is_page(array(42,'about-me','About Me And Joe')) 
   当一个Page页面的ID是42或是文章别名是about-me,或是标题为About Me And Joe时,返回True。注意:版本2.5支持数据参数。

子页面测试

还没有is_subpage()函数,你可以用一段小代码进行测试:

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
}
?>

你可以添加这个函数到你的模板函数functions.php:

扫描二维码关注公众号,回复: 11164773 查看本文章
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
};

调用is_true('id'),来看你这个页面在不在一个树页面中。在下面的示例中,is_tree('2')将替换第一个if语句的条件里的"is_page('about') || $post->post_parent == '2'" 。注意,如果你有不止一级的页面,父页面是直接上级,但不是树的顶级。

如果你要测试是不是一个Page页面,或是不是一个子页面(比如:不同的页面,会有不同的级别标题),从管理页面获取这个页面的ID,你可以使用下面的代码:

<?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  
}	

?>

如果你会测试很多次Page页面,你最好把这段代码做成函数加到functions.php中,这样便于维护,也利于调用。

页面模版

从版本2.5开始,允许你判断你是否使用Page模板。

is_page_template() 
   如果使用页面模板,则返回True。
is_page_template('about.php') 
   如果使用页面模板‘about’,则返回True。注意,这个函数参数,不像其他的参数,如果你想使用一个页面模板,你需要指定文件名加后缀。

分类页面

is_category() 
   如果是一个类别归档页面,则返回True。
is_category('9') 
   当指定分类ID为9时,则返回True。
is_category('Stinky Cheeses') 
   当分类类别名称为“Stinky Cheeses”时,则返回True。
is_category('blue-cheese') 
   当分类归档页面的别名(slug)为“blue-cheese”时,则返回True。
is_category(array(9,'blue-cheese','Stinky Cheeses')) 
   当文章类别的ID为9、别名(slug)为“blue-cheese”或名称为“Stinky Cheese”时,返回True。版本2.5开始支持数组参数。
in_category('5') 
   当一个文章属于类别ID为5的分类时,则返回True。

注意:使用时一定要检查你的拼写,以is或in开头的函数,在使用上是有很大不同的。

另见 is_archive() 和 Category Templates.

标签页面

is_tag() 
当为标签归档页时,返回True。
is_tag('mild') 
   标签别名为“mild”时,返回True。
is_tag(array('sharp','mild','extreme')) 
   标签别名为“sharp”,“mild”,或是“extreme”时,返回True。注意:数组参数在版本2.5后支持。
has_tag() 
   判断当前文章是否有含有标签,必须放到循环中使用。注意:has_tag是在版本2.6后加入的。
has_tag('mild') 
   当前文章有标签mild时,返回True。
has_tag(array('sharp','mild','extreme')) 
   当前文章多个标签时,返回True。

作者页面

is_author() 
   显示作者页面时,返回True。
is_author('4') 
   归档页面中,作者ID为4时,返回True。
is_author('Vivian') 
   当归档页面作者的昵称为“Vivian”时,返回True。
is_author('john-jones') 
   当归档页面作者的名称为“john-jones”时,返回True。
is_author(array(4,'john-jones','Vivian')) 
   当归档页面作者的ID为4、或名称为“john-jones”、或是昵称为“Vivian”时,返回True。注意:数组参数的支持是从版本2.5开始的。

日期页面

is_date() 
   当任何基于时间的归档页面显示时,返回True。(如:月、年、日或是时间归档)。
is_year() 
   判断是否为年归档。
is_month() 
   判断是否为月归档。
is_day() 
   判断是否为日归档。
is_time() 
   判断是否为时分秒归档。

存档页面

is_archive() 
  当为任何存档类型的页面时,返回True。分类、标签、作者和日期页面为所有类型的存档。

搜索结果页面

is_search() 
   当为一个查询结果的存档页面时,返回True。

404页面

is_404() 
   当一个页面出现404时,返回True。

分页页面

is_paged() 
   判断是否为分离的页面。将一个存档页面分成多个页面。这不是指文章的分页,而是一个大文章被分成了几个小块。

是否为附件页面

is_attachment() 
   当一个页面为附件文章或Page页面时,返回True。附件可以是图片或是其他通过编辑器上传的文件。它可以显示在他们自己的页面或模板上。具体请看图片或文件附件。

是否为文章页、页面或附件

is_singular() 
   当is_single()、is_page()、或is_attachement()任意一个返回True时,is_singular()函数都会返回True。

在订阅源中

is_feed() 
   
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.

在引用通告中

is_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.

是否为预览

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

是否有摘要

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

是否在循环内

in_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_active_sidebar() 
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.

使用示例

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

帮助性404页面

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.

在主题的 footer.php 文件

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 putwp_reset_query before the conditional test in the footer. For example:

<?php
wp_reset_query();
if (is_page('2') ) {
echo 'this is page 2!';
} 
?>
原创文章 22 获赞 20 访问量 18万+

猜你喜欢

转载自blog.csdn.net/lw001x/article/details/52079021