条件标签可以在经典主题的模板文件中使用,以改变内容的显示,这取决于当前页面符合的条件。它们告诉WordPress在特定条件下显示什么代码。条件标签通常与PHP的if/else条件语句一起工作。
代码的开始是检查一个语句是true还是false。如果发现该语句为true,则执行第一组代码。如果是false,则跳过第一组代码,转而执行第二组代码(在else之后)。
例如,你可以检测用户是否已经登录,然后根据结果提供不同的问候语。
if ( is_user_logged_in() ): echo 'Welcome, registered user!'; else: echo 'Welcome, visitor!'; endif;
注意这些标签与WordPress模板层次结构的密切关系。
在哪里使用条件标签
要让条件标签修改数据,信息必须已经从数据库中检索出来,也就是说,查询必须已经运行。如果你在有数据之前使用条件标签,就没有什么可以问if/else语句的。
需要注意的是,WordPress会在查询运行前加载functions.php,所以如果你直接在该文件中包含一个条件标签,它将不会起作用。
实现条件标签的两种方法
- 把它放在一个模板文件中
- 在 functions.php 中创建一个函数,应用动作或过滤器钩子,在稍后的时间点触发
条件
下面列出了条件标签以及它们被证明为true的条件,注意可以接受参数的标签。
主页面
当主博客页面被显示时,该条件返回为true,通常是以标准的反向时间顺序。如果你的主页被设置为静态页面,那么这个条件只会在你在 "设置">"阅读" 中设置为 "文章页" 的页面上证明为true。
首页
当网站的首页被显示时,这个条件会返回true,不管它是被设置为显示文章还是静态页面。
以下情况返回true:
- 正在显示主博客页面,且
- 设置 > 阅读 -> 首页显示选项被设置为您的最新文章
或者
- 当 "设置">"阅读"->"首页显示"被设置为 "静态页面",且
- 主页的值是当前正在显示的页面
管理面板
当显示仪表板或管理面板时,该条件返回true
单一文章页面
当任意单一的文章(或附件、自定义文章类型)被显示时返回true。如果你在一个页面上,这个条件返回false。
is_single( ‘17’ )
is_single()也可以通过ID和其他参数检查某些文章。上面的例子表明,当文章17作为单页显示时,返回true。
is_single( ‘Irish Stew’ )
参数也包括文章的标题。在这种情况下,当标题为 "Irish Stew" 的文章作为单页显示时,它返回true。
is_single( ‘beef-stew’ )
当slug是"beef-stew"的文章作为单页显示时,为true。
is_single( array( 17, ‘beef-stew’, ‘Irish Stew’ ) )
当显示的单页文章ID为17,或post_name为“beef-stew”,或post_title为“Irish Stew”,返回true
is_single( array( 17, 19, 1, 11 ) )
当显示的单页文章ID为17, 19, 1, 11任一,返回true
is_single( array( ‘beef-stew’, ‘pea-soup’, ‘chilli’ ) )
当显示的单页文章post_name为“beef-stew”, “pea-soup”, “chilli”任一,返回true
is_single( array( ‘Beef Stew’, ‘Pea Soup’, ‘Chilli’ ) )
当显示的单页文章post_title为“Beef Stew”, “Pea Soup”, “Chilli”任一,返回true
注意:这个函数不区分文章ID、文章标题或文章slug。如果请求ID是17的文章,同样会显示标题为 "17 "的文章,这也适用于slug为 "17 "的文章。
单文章, 单页面, 附件页
任一is_single、is_page和is_attachment都返回true,它允许对这些文章类型进行检测。
置顶文章
如果当前文章选中"置顶这篇文章",则返回true。在此例子中,没有给出文章的ID参数,所以使用循环中文章的ID。
is_sticky( ’17’ )
当文章17被置顶时,返回true
文章类型
你可以通过在条件中加入get_post_type()来检测当前文章是否属于某种类型。这不是真正的条件标签,但它会返回当前文章的注册类型。
if ( ‘book’ == get_post_type() ) …
如果给定的文章类型是已注册的类型,则返回true。这并非检测文章是否是某个post_type。注意:这个函数取代了在3.0开发中短暂存在的名为is_post_type函数。
文章类型是否有分层
is_post_type_hierarchical( $post_type )
如果这个$post_type在注册时被设置为支持分层,则返回true。
is_post_type_hierarchical( ‘book’ )
如果“book”类型被注册为支持分层,则返回true。
文章类型存档页
在任意文章类型的存档页中返回true
is_post_type_archive( $post_type )
在符合$post_type的文章类型存档页上,返回true($post_type可以是单个或一个数组)
要让文章类型支持存档页,在注册时使用'has_archive' => true
任何包含文章的页面
在WordPress循环中处理的当前文章允许评论
当WordPress循环中正在处理的当前文章允许pings
页面
本节指的是WordPress页面,而不是你的博客的任何普通网页,或者换句话说,是指内置的类型post_type="page"。
任意页面被显示时。
is_page( ’42’ )
当显示的页面ID为42
is_page( ‘About Me And Joe’ )
当显示的页面post_title为 “About Me And Joe”
is_page( ‘about-me’ )
当显示的页面post_name (slug)为 “about-me”
is_page( array( 42, ‘about-me’, ‘About Me And Joe’ ) )
当显示的页面 ID = 42,post_name = “about-me” 或 post_title = “About Me And Joe” 任一则返回true
is_page( array( 42, 54, 6 ) )
当显示的页面 ID = 42,ID = 52,或 ID = 6,任一则返回true
检测分页
你可以使用这段代码来检查你是否在一个使用<!--nextpage-->
快速标签分页的文章或页面中的第n页。这很有用,例如,如果你希望只在被分成几页的文章的第一页显示元数据。
示例1
<?php $paged = $wp_query->get( 'page' ); if ( ! $paged || $paged < 2 ) { // This is not a paginated page (or it's simply the first page of a paginated page/post) } else { // This is a paginated page. } ?>
示例2
<?php $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : false; if ( $paged == false ) { // This is not a paginated page (or it's simply the first page of a paginated page/post) } else { // This is a paginated page. } ?>
检测子页面
没有is_subpage()这样的函数,但你可以用一点代码检测一下
片段 1
<?php global $post; // if outside the loop if ( is_page() && $post->post_parent ) { // This is a subpage } else { // This is not a subpage } ?>
你可以使用片段2中的代码创建自己的is_subpage()函数,把它添加到 functions.php 文件中。它检测父页面的方式与片段1相同,但如果有父页面,将返回其ID,如果没有,则返回false。
片段 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 return $post->post_parent; // return the ID of the parent post } else { // there is no parent so ... return false; // ... the answer to the question is false } }
如果你需要经常检测子页面,建议使用像片段2那样的函数,而不是使用片段1那样的简单测试。
为了检测一个页面的父级是否是一个特定的页面,例如 "关于"(父页面id 默认 2),我们可以使用片段3中的检测。这些检测会检查我们是否在浏览相关的页面,以及是否在浏览任何子页面。这对于设置网站不同部分的特定变量很有用,比如不同的横幅图片,或者不同的页眉。
片段 3
<?php if ( is_page( 'about' ) || '2' == $post->post_parent ) { // the page is "About", or the parent of the page is "About" $bannerimg = 'about.jpg'; } elseif ( is_page( 'learning' ) || '56' == $post->post_parent ) { $bannerimg = 'teaching.jpg'; } elseif ( is_page( 'admissions' ) || '15' == $post->post_parent ) { $bannerimg = 'admissions.jpg'; } else { $bannerimg = 'home.jpg'; // just in case we are at an unclassified page, perhaps the home page } ?>
片段4是一个函数,可以让你更容易地进行上述检测。如果我们浏览的是相关的页面(如 "关于")或它的一个子页面(如父页ID为 "2 "),这个函数将返回true
片段 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($pid) ) return true; // we're at the page or at a sub page $anc = get_post_ancestors( $post->ID ); foreach ( $anc as $ancestor ) { if( is_page() && $ancestor == $pid ) { return true; } } return false; // we aren't at the page, and the page is not an ancestor }
在你的 functions.php 文件中加入片段 4,并调用 is_tree( 'id') 来查看当前页面是否为“page”,或者是该页的子页。在片段3中,is_tree( '2') 将在第一个if标签中替换 "is_page( 'about') || '2' == $post->post_parent"
请注意,如果你有一个以上级别的页面,那么父级页面就是正上方的那个,而不是层次结构中最顶端的那个。
是否页面模板
允许你确定你是否在一个页面模板中,或者是否正在使用一个特定的页面模板。
是否正在使用一个页面模板?
is_page_template( ‘about.php’ )
是否正在使用'about'页面模板? 注意,与其他条件式不同,如果你想指定一个特定的页面模板,你需要使用文件名,如about.php或my_page_template.php。
注意:如果文件是在一个子目录下,你也必须包括这个子目录。这意味着这应该是与主题有关的文件路径以及文件名,例如 "page-templates/about.php"。
分类页面
当显示一个分类存档页时
is_category( ‘9’ )
当显示分类9存档页时
is_category( ‘Stinky Cheeses’ )
当显示分类名称“Stinky Cheeses”的存档页时
is_category( ‘blue-cheese’ )
当显示分类slug “Stinky Cheeses”的存档页时
is_category( array( 9, ‘blue-cheese’, ‘Stinky Cheeses’ ) )
当显示分类term_ID 9或slug “blue-cheese”或名称“Stinky Cheeses”的文章时,返回true
in_category( ‘5’ )
如果当前文章在所指定的分类id里,返回true
in_category( array( 1, 2, 3 ) )
如果当前文章在分类 1, 2, 或 3里,返回true
! in_category( array( 4, 5, 6 ) )
如果当前文章“不”在分类4, 5, 6里
注意:一定要检查你的拼写,"is"或"in"之间有很大区别。
标签页
当任何标签存档页正在显示时。
is_tag( ‘mild’ )
当显示标签slug ‘mild’ 的存档页时
is_tag( array( ‘sharp’, ‘mild’, ‘extreme’ ) )
当显示的标签存档页有“sharp”, “mild”, 或 “extreme” 任一slug时,返回true
当当前文章有标签,必须在循环中使用
has_tag( ‘mild’ )
当前文章有标签 ‘mild’.
has_tag( array( ‘sharp’, ‘mild’, ‘extreme’ ) )
当前文章有数组中的任一标签
分类法页面
当任意分类法存档页显示时
is_tax( ‘flavor’ )
当分类法flavor的存档页显示时
is_tax( ‘flavor’, ‘mild’)
当有slug ‘mild’的分类法flavor的存档页显示时
is_tax( ‘flavor’, array( ‘sharp’, ‘mild’, ‘extreme’ ) )
当有slug “sharp”, “mild”, 或 “extreme”的分类法flavor的存档页显示时返回true
检查当前文章是否有给定的分类法项目。第一个参数应该是一个空字符串。它希望第二个参数是一个分类法slug/name。
has_term( ‘green’, ‘color’ )
当前文章有分类法‘color’的‘green’项目时,返回true
has_term( array( ‘green’, ‘orange’, ‘blue’ ), ‘color’ )
当前文章有数组中的任一项目时
已注册的分类法
当一个特定的分类法通过register_taxonomy()注册时。以前的is_taxonomy(),在3.0版本中被废弃。
作者页面
当一个作者页面显示时
is_author( ‘4’ )
当作者ID=4的存档页显示时
is_author( ‘Vivian’ )
当作者Nickname为“Vivian”的存档页显示时
is_author( ‘john-jones’ )
当作者Nicename为“john-jones”的存档页显示时
is_author( array( 4, ‘john-jones’, ‘Vivian’ ) )
当作者ID=4、user_nicename=“john-jones”或nickname=“Vivian”的存档页显示时
多作者的网站
当网站有一个以上的作者发表文章时,在3.2版本中可用。
日期页面
当任何基于日期的存档页被显示时(即月、年、日或基于时间的存档)。
显示年度存档页时
显示月度存档页时
显示每日存档页时
显示每小时、每分钟或每秒钟的档案时
如果根据发布日期,今天是新的一天,应在循环内使用。
任意存档页
当任意类型的存档页被显示时。类别、标签、作者和基于日期的页面都是。
搜索结果页
搜索结果存档页显示时
404 页面
“HTTP 404: 没有找到”错误页面显示时
隐私政策页面
显示隐私政策页面时
附件页
当文章或页面的附件文件被显示时。附件是通过文章编辑器的上传工具上传的图像或其他文件。附件可以显示在他们自己的 "页面 "或模板上。
单页面、文章单页、附件页
当is_single()
, is_page()
或 is_attachment()
任一返回true时
is_singular( ‘book’ )
当浏览的文章为自定义文章类型 book 时
is_singular( array( ‘newspaper’, ‘book’ ) )
当浏览的文章为自定义文章类型 newspaper 或 book 时
聚合
当所请求的网站是聚合时。这个标签通常不是用户使用的,它被WordPress内部使用,并供插件开发者使用。
Trackback
当请求的网站是WordPress的钩子进入其Trackback引擎。这个标签通常不被用户使用,它被WordPress内部使用,并供插件开发者使用。
预览
当文章以草稿预览模式显示时
有摘录
当前文章有摘录时
has_excerpt( 42 )
文章 ID=42 有摘录时
<?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 } ?>
其他用法
当你需要隐藏自动显示的摘录,只显示你的文章摘录时
<?php if ( ! has_excerpt() ) { echo ''; } else { the_excerpt(); } ?>
替换自动摘录的文本或代码
<?php if ( ! has_excerpt() ) { // your text or code } ?>
有指定导航菜单
已注册的导航菜单位置是否有指定菜单
返回: 有指定(true)或没有(false)
在循环内
检查你是否在 "循环内"。这个条件对插件作者很有用,当你在循环内时,这个条件返回为true
边栏是否启用
检测给定的边栏是否启用(使用中),如果边栏(根据name, id, 或 number来确定)正在使用中返回true,否则返回false.
网络的一部分(多站点)
检查当前站点是否在WordPress多站点安装中
主站点(多站点)
确定一个站点是否是网络中的主要站点
网络管理员(多站点)
确定一个用户是否是网络(超级)管理员
插件启用
检测一个插件是否启用
子主题
检查是否正在使用一个子主题
主题支持某项功能
检查各种主题功能是否存在
示例
这里有一些工作样本来演示如何使用这些条件标签。
文章单页
这个例子展示了如何使用is_single()
来显示一些特定的东西,只有在查看文章单页时。
if ( is_single() ) { echo 'This is just one of many fabulous entries in the ' . single_cat_title() . ' category!'; }
另一个例子是如何在循环中使用条件标签。选择在index.php中显示内容或摘录,当这是一个显示单篇文章或主页时。
if ( is_home() || is_single() ) { the_content(); } else { the_excerpt(); }
当你需要显示一个代码或元素时,在一个不是主页的地方。
<?php if ( ! is_home() ) { // Insert your markup ... } ?>
多重条件检查
你可以使用PHP运算符在一个if语句中评估多个条件。
如果你需要检查条件的组合是否评估为true或false,这就很方便。
// Check to see if any of 2 conditionals are met if ( is_single() || is_page() ) { // If it's a single post or a single page, do something special } if ( is_archive() && ! is_category( 'nachos' ) ) { // If it's an archive page for any category EXCEPT nachos, do something special }
// Check to see if 3 conditionals are met if ( $query->is_main_query() && is_post_type_archive( 'products' ) && ! is_admin() ) { // If it's the main query on a custom post type archive for Products // And if we're not in the WordPress admin, then do something special } if ( is_post_type_archive( 'movies' ) || is_tax( 'genre' ) || is_tax( 'actor' ) ) { // If it's a custom post type archive for Movies // Or it's a taxonomy archive for Genre // Or it's a taxonomy archive for Actor, do something special }
基于日期的差异
如果有人按日期浏览我们的网站,让我们用不同的颜色来区分不同年份的帖子。
<?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"><?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>
可变的侧边栏内容
这个例子将根据读者当前查看的页面,在你的侧边栏显示不同的内容。
<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 wp_list_categories( 'optionall=0&sort_column=name&list=1&children=0' ); } elseif ( is_category() ) { // we're looking at a single category view, so let's show _all_ the categories wp_list_categories( 'optionall=1&sort_column=name&list=1&children=1&hierarchical=1' ) } 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 "This is my about page!"; } elseif ( is_page( 'Colophon' ) ) { echo "This is my colophon page, running on WordPress " . bloginfo( 'version' ) . ""; } else { // catch-all for other pages echo "Vote for Pedro!"; } } else { // catch-all for everything else (archives, searches, 404s, etc) echo "Pedro offers you his protection."; } // That's all, folks! ?> </div>
在主题footer.php文件中
有时,在其他模板中进行的查询,如sidebar.php可能会破坏某些条件标签。例如,在header.php中,一个条件标签可以正常工作,但在主题的footer.php中却不工作。诀窍是把wp_reset_query放在页脚的条件测试之前。比如说:
<?php wp_reset_query(); if ( is_page( '2' ) ) { echo 'This is page 2!'; } ?>