WordPress网站开发常用条件标签列表

| | Comments (3)



如果自己开发wordpress模板,这些条件标签是不可缺少的,国际为英文,中文大家可以百度条件标签,很多资料,这里列一些,可能不全:

条件标签

简介

在模板文件中,我们可以根据能与某页面相匹配的条件,利用条件标签更改该页面上将要显示的内容以及内容的显示方式。例如,如果希望在博客首页文章列表的上方显示一段文字,可以利用 is_home()条件标签轻松实现这一效果。 注意:条件标签与WordPress模板层级密切相关。

条件

所有条件标签都会检查是否有条件可符合相应页面,之后返回TRUE或者FALSE。下面列出能够使各个条件标签返回TRUE的条件:

主页

is_home() 显示博客主页时,该条件标签返回TRUE。 注意:如果将静态页面作为博客主页,该标签会在显示日志页面时返回TRUE。

标题页

is_front_page() 显示博客头版消息(无论是日志或是页面)。当系统显示博客主页且管理面板的设置>阅读菜单下"主页显示为"选项设为最近发表的文章",或者'设置>阅读菜单下"主页显示为"选项设为且"主页"是当前被显示的页面时,is_front_page()标签返回TRUE。注意:该标签在WordPress 2.5版本中首次亮相。

管理栏

is_admin() 显示控制板或管理栏时,is_admin()标签返回TRUE。

单篇日志页面

is_single() 显示任何单独一篇日志时,is_single()标签返回TRUE。 is_single('17') 显示编号为17的单篇日志时,该标签返回TRUE。 is_single('Irish Stew') 显示标题为"Irish Stew"的单篇日志时,该标签返回TRUE。 is_single('beef-stew') 显示别名为"beef-stew"的单篇日志时,该标签返回TRUE。 is_single(array(17,'beef-stew','Irish Stew')) 当所显示的单篇日志编号为17,或post_name为"beef-stew",或post_title为"Irish Stew"时,is_single(array(17,'beef-stew','Irish Stew'))标签返回TRUE。注意:数组功能首次出现于WordPress 2.5。

置顶日志

is_sticky() 若当前日志的"Stick this post to the front page(将该日志在首页置顶)"复选框被选中,is_sticky()返回TRUE。此时标签没有提供日志的编号变量,因此系统会用到WordPress主循环the Loop的日志编号。注意:该标签首次出现于WordPress 2.7。 is_sticky('17') 若编号为17的日志被置顶,该标签返回TRUE。

评论弹出式窗口

is_comments_popup() 显示的内容在评论弹出式窗口中时,该标签返回TRUE。

任何包含日志的页面

comments_open() 若评论允许当前日志在WordPress主循环中运行,该标签返回TRUE。 pings_open() 若引用通告ping允许当前日志在WordPress主循环中运行,该标签返回TRUE。

PAGE页面

这里的PAGE指的是WordPress的"页面"功能,而不是博客生成的网页。 is_page() 显示任何一篇页面时,该标签返回TRUE。 is_page('42') 显示编号为42的页面时,该标签返回TRUE。 is_page('About Me And Joe') 若显示的页面的post_name为"About Me And Joe",该标签返回TRUE。 is_page('about-me') 若显示的页面的post_name(别名)为"about-me",该标签返回TRUE。 is_page(array(42,'about-me','About Me And Joe')) 当所显示的页面编 号为42,或post_name为"about-me",或post_title为"About Me And Joe"时,is_page(array(42,'about-me','About Me And Joe'))标签返回TRUE。注意:数组功能首次出现于WordPress 2.5。

子页面

目前is_subpage()函数尚未出现,但我们可以用以下代码来判断某页面是否子页面:
<?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文件中:
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_tree('id')来判断页面是否在目录树中。在下面的代码示例中,我们可以用is_tree('2')来代替第一个if标签中 的"is_page('about') || $post->post_parent == '2'"。注意,如果有两层以上页面,父页面指的是子页面的上一级页面而不是顶级页面。 如果要判断当前页面是否是某个特定页面或者是否是某一页面的子页面(例如在一个基于页面的网站的不同版块上显示不同banner广告),可以先从后台获取父页面的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  
}	

?>
如果希望进行多次页面判断,最好创建一个长期函数(is_cpage())。例如,如果将is_subpage()引入wp,用户就可以在functions.php文件中修改函数而不必做其它多余改动了。

Is a Page模板

自WordPress 2.5版起,用户可以通过Is a Page模板来判断目前是否位于页面模板中,或是否在使用某个特定的页面模板。 is_page_template() 目前是否使用了页面模板 is_page_template('about.php') 是否使用页面模板'about'?注意:页面模板与其它条件标签有所不同,用户需要使用文件名(如 about.php 或my_page_template.php)来指定某个页面模板。

类别页面

is_category() 显示类别存档页面页面时,is_category()标签返回TRUE。 is_category('9') 显示类别ID为9的存档页面时,is_category('9')标签返回TRUE。 is_category('Stinky Cheeses') 显示雷冰名称为"Stinky Cheeses"的存档页面时,is_category('Stinky Cheeses')标签返回TRUE。 is_category('blue-cheese') 显示类别别名为"blue-cheese"的存档页面时,is_category('blue-cheese') 标签返回TRUE。 is_category(array(9,'blue-cheese','Stinky Cheeses')) 当所显示的类别存档term_ID为9,或别名为"blue-cheese",或类别名称为"Stinky Cheeses"时,is_category(array(9,'blue-cheese','Stinky Cheeses')) 标签返回TRUE。注意:数组功能首次出现于WordPress 2.5。 in_category('5') 若当前日志属于某个指定ID的类别,in_category('5') 标签返回TRUE。 注意:判断条件是否符合时,请不要将"in"写作"is",两者有很大区别。

标签页面

is_tag() 显示标签存档页面时,is_tag() 会返回TRUE。 is_tag('mild') 显示别名为"mild"的标签的存档页面时,is_tag('mild') 返回TRUE。 is_tag(array('sharp','mild','extreme')) 若显示的标签别名为"sharp"、 "mild"或"extreme",is_tag(array('sharp','mild','extreme')) 返回TRUE。注意:数组功能首次出现于WordPress 2.5。 has_tag() 若当前日志有标签,has_tag()返回TRUE。该标签必须用在WordPress主循环 中。注意:has_tag标签首次出现在WordPress 2.6版中。 has_tag('mild') 若当前日志标签为"mild",as_tag('mild') 返回TRUE。 has_tag(array('sharp','mild','extreme')) 若当前日志标签为数组中任一个标签,has_tag(array('sharp','mild','extreme')) 返回TRUE。

作者页面

is_author() 显示作者页面时,is_author()返回TRUE。 is_author('4') 若显示的是ID为4的作者的存档页面,is_author('4') 返回TRUE。 is_author('Vivian') 若显示的是用户名为"Vivian"的作者的存档页面,is_author('Vivian') 返回TRUE。 is_author('john-jones') 若显示的是用户名为"john-jones"的作者的存档页面,is_author('john-jones') 返回TRUE。 is_author(array(4,'john-jones','Vivian')) 若页面的用户名ID为4,或user_nicename为"john-jones",或用户名为 "Vivian",is_author(array(4,'john-jones','Vivian')) 返回TRUE。注意:数组功能首次出现于 WordPress 2.5。

日期页面

is_date() 显示日期相关的存档页面(如月存档,年存档,日存档等基于时间的存档页面)时,is_date() 返回TRUE。 is_year() 显示年存档信息时,is_year() 返回TRUE。 is_month() 显示月存档信息时,is_month() 返回TRUE。 is_day() 显示日存档信息时,is_day() 返回TRUE。 is_time() 显示某时、某分或某秒的存档信息时,is_time() 返回TRUE。

存档页面

is_archive() 无论显示何种存档页面时,is_archive() 都会返回TRUE。类别存档、标签存档、作者存档以及日期存档页面都属于存档页面。

搜索结果页面

is_search() 显示搜索结果页面时,is_search() 返回TRUE。

404 Not Found页面

is_404() 出现"HTTP 404: Not Found"错误后所显示的页面。

分页型页面

is_paged() 当前页面需要分页显示时,is_paged()返回TRUE。分页型页面是指某个存档页面或需要分成若干页显示的主页,显示第二页以及之后页面时,is_paged()返回TRUE。有些日志或页面使用<!--nextpage-->快速标签分页显示其内容,这些日志和页面都不属于分页型页面。

附件

is_attachment() 显示某篇日志或页面的附件文件时,is_attachment() 会返回TRUE。附件可以是一张图片,也可以是用户所上传的其它文件。附件能够显示在"页面"或模板上。更多信息请参阅"图片和附件的使用"。

独立页面、单篇日志或附件

is_singular() 当is_single(), is_page() 或is_attachment()返回TRUE时,is_singular() 也返回TRUE。

新闻聚合器

is_feed() 网站需要使用RSS订阅时,is_feed()返回TRUE。一般情况下会用到is_feed()标签的是WordPress系统和插件开发人员,普通WordPress用户很少用到is_feed()标签。

引用通告(Trackback)

is_trackback() 网站需要将WordPress钩子勾入Trackback。一般情况下会用到这个标签的是WordPress系统和插件开发人员,普通WordPress用户很少用到is_feed()标签。

预览

is_preview() 若目前页面是在草稿状态下显示的(即预览),is_preview() 返回TRUE。

Has An Excerpt (摘要)

as_excerpt() 当前日志要显示摘要时,as_excerpt() 返回TRUE。 has_excerpt('42') 若ID为42的日志要显示摘要时,has_excerpt('42') 返回TRUE。
<?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
}
?>

Inside The Loop (在WordPress主循环中)

in_the_loop() 判断目前是否在WordPress主循环中。该标签适用于插件开发人员,若目前在WordPress主循环中,该标签返回TRUE。

Is Sidebar Active(侧边栏是否被激活)

is_active_sidebar() 检查现有侧边栏是否被激活(是否启用)。若侧边栏被启用,is_active_sidebar() 返回TRUE,否则返回FALSE。该条件函数引入于WordPress 2.8版。

实例

下面是一些对条件标签的实际应用。

单篇日志

下面这个例子向我们展示了怎样用is_single()为某个单独日志页面显示独特内容:
if (is_single())
{
     echo 'This is just one of many fabulous entries in the ' . single_cat_title() . ' 
category!';
}

区分基于日期的页面

如果访问者按日期浏览网站,我们可以用不同颜色标注不同年份的日志或其它信息:
<?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>

可视化的侧边栏内容

本例根据访问者当前浏览的界面,在侧边栏上显示不同内容。
<!-- 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页面

设计Error 404页面中的编写友好的错误信息部分有一个使用PHP条件函数isset()的示例。

动态菜单高亮

动态菜单高亮中介绍了如何用条件标签在菜单中激活当前页面的高亮显示部分。

在主题的footer.php文件中

有时在模板文件(如sidebar.php)中执行的查询可能破坏一些条件标签。例如,某个条件标签可以在header.php文件中正常运行,但却不能适应主题的footer.php文件。这时可以在页脚中把wp_reset_query放条件检验前:
<?php
wp_reset_query();
if (is_page('2') ) {
echo 'this is page 2!';
} 
?>

条件标签索引

按字母顺序排列:
  • 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_time
  • is_trackback
  • is_year
  • pings_open

3 Comments

正是用得着。

学习了,记住这些对于一个wordpress站长来说是很重要的。

有用,比官方文档还详细。

Leave a comment

Archives

Ads by google

Pages

Powered by Movable Type 4.24-en

About this Entry

This page contains a single entry by 单点日志 published on February 24, 2010 9:38 PM.

Wordpress自动定时指定时间发布文章对网站优化的好处 was the previous entry in this blog.

发现宝淘宝客牛站是如何被百度降权分析 is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.