条件标签可以在经典主题的模板文件中用于根据当前页面匹配的条件来改变内容的显示方式。它们告诉 WordPress 在特定条件下应显示什么代码。条件标签通常与 PHP if /else 条件语句一起工作。

代码首先检查某个陈述是否为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 中创建一个函数并将其钩入稍后触发的操作/过滤器

条件说明

以下是每个以下条件语句证明为真的条件列表。可接受参数的标签已注明。

主页

is_home()

当显示主博客页面时,此条件返回 true(真),通常按标准逆时间顺序排列。如果您的首页已设置为静态页面,则仅在您将在“设置 > 阅读”中设置的“文章页面”上证明为真。

前台页

is_front_page()

当显示网站的前台页面时,此条件返回 true(真),无论其是设置为显示文章还是静态页面。

在以下情况下返回 true:

  1. 正在显示主博客页面并且
  2. “设置 > 阅读 -> 前台页显示"选项已设置为您的最新文章

或者

  1. 当“设置 > 阅读 -> 前台页显示”设置为静态页面并且
  2. 前台页面的值是正在显示的当前页面。

管理面板

is_admin()

当显示仪表板或管理面板时,此条件返回 true(真)。

单个文章页面

is_single()

当显示任何单篇文章(或附件,或自定义文章类型)时返回 true。如果您在页面上,此条件将返回 false。

is_single( '17' );

is_single() 还可以根据 ID 和其他参数检查某些文章。上面的示例在显示单篇文章时证明为真。

is_single( 'Irish Stew' );

参数包括帖子标题等。在这种情况下,当显示标题为“Irish Stew"的单篇文章时,它证明为真。

is_single( 'beef-stew' );

当显示文章名称(slug)为“beef-stew”的作为单篇文章显示的文章时证明为真。

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、文章 ID = 19、文章 ID = 1 或文章 ID = 11 时返回 true。

is_single( array( 'beef-stew', 'pea-soup', 'chilli' ) );

当显示的单个帖子是 post_name“beef-stew"、post_name“pea-soup”或 post_name“chilli”时返回 true。

is_single( array( 'Beef Stew', 'Pea Soup', 'Chilli' ) );

当显示的单个帖子是 post_title 为"Beef Stew"、post_title 为“Pea Soup”或 post_title 为“Chilli”时返回 true。

注意:此函数不区分文章 ID、文章标题或文章名称。如果请求了名为"17"的文章,将显示文章 ID 为 17 的文章。据推测,对于 slug(名称)为"17"的帖子也是如此。

单个文章、页面或附件

is_singular()

对任何 is_single、is_page 和 is_attachment 返回 true。它允许测试帖子类型。

置顶文章

is_sticky()

如果当前帖子的“将此帖子粘在前台”复选框已勾选,则返回 true。在此示例中,未给出文章 ID 参数,因此使用循环文章的帖子 ID。

is_sticky( '17' );

当第 17 篇文章被视为置顶文章时返回 true。

文章类型

get_post_type()

您可以通过在条件中包含 get_post_type() 来测试当前帖子是否为特定类型。它实际上不是一个条件标签,但它返回当前帖子的注册文章类型。

if ( 'book' == get_post_type() ) { ... }

post_type_exists()

如果给定的文章类型是已注册的文章类型,则返回 true。这并不测试帖子是否为某个 post_type(文章类型)。注意:此函数替换了 3.0 开发版本中短暂存在的名为 is_post_type 的函数。

文章类型为层级结构

is_post_type_hierarchical( $post_type )

如果此$ post_type 在注册时已设置为具有层级结构支持,则返回 true。

 is_post_type_hierarchical( 'book' );

当 book 文章类型被注册为具有层级结构支持时返回 true。

文章类型归档页

is_post_type_archive()

在任何文章类型归档页面返回 true。

is_post_type_archive( $post_type );

当在匹配$ post_type 的文章类型归档页面上时返回 true(可以是单个文章类型或文章类型的数组)。

要启用文章类型归档,请在注册文章类型时使用‘has_archive’=> true。

任何包含文章的页面

comments_open()

当在 WordPress 循环中处理当前文章时允许评论。

pings_open()

当在 WordPress 循环中处理当前文章时允许 Ping(回显)。

“页面”页

本节指的是 WordPress 页面,而不是来自您的博客的任何通用网页,或者换句话说是指内置的post_type'page'。

is_page()

当显示任何页面时。

is_page( '42' );

当显示第 42 页(ID)时。

is_page( 'About Me And Joe' );

当显示post_title为“关于我和乔”的页面时。

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为“关于我和乔”时返回 true。

is_page( array( 42, 54, 6 ) );

当显示的页面是 文章 ID= 42,或文章 ID= 54,或文章 ID= 6 时返回 true。

测试分页页

您可以使用此代码检查您是否处于被

<!--nextpage-->
QuickTag 分割成页面的文章或页面中的第 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.
endif;
示例 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.
endif;

测试子页面

没有 is_subpage() 函数,但您可以使用少量代码进行测试:

<?php
global $post; // if outside the loop

if ( is_page() && $post->post_parent ) :
	// This is a subpage
else :
	// This is not a subpage
endif;

片段 1

您可以使用 Snippet 2 中的代码创建自己的 is_subpage() 函数。将其添加到您的 functions.php 文件中。它以与 Snippet 1 相同的方式测试父页面,但如果有的话将返回页面的 ID,如果没有则返回 false。

片段 2

<?php
function is_subpage() {
	// Load details about this page.
	global $post;

	// Test to see if the page has a parent
	if ( is_page() && $post->post_parent ) {
		// return the ID of the parent post
		return $post->post_parent;

	// there is no parent so ...
	} else {
		// ... the answer to the question is false
		return false;
	}
}

如果您计划经常测试子页面,则建议使用 Snippet 2 中的函数而不是使用简单的 Snippet 1 中的测试。

如果要测试页面的父页面是否为特定页面(例如“关于”),我们可以使用 Snippet 3 中的测试。这些测试检查我们是否正在查看相关页面,以及我们是否正在查看任何子页面。这对于为网站的不同部分设置特定的变量很有用,因此可以显示不同的横幅图像或标题。

片段 3

<?php
// The page is "About", or the parent of the page is "About".
if ( is_page( 'about' ) || '2' == $post->post_parent ) {
	$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';
// Just in case we are at an unclassified page, perhaps the home page
} else {
	$bannerimg = 'home.jpg';
}

片段 4 是一个函数,允许您更轻松地执行上述测试。如果我们在查看相关页面(即“关于”)或其子页面之一(即 ID 为"2"的父页面的页面),则该函数将返回 true。

片段 4

将 Snippet 4 添加到您的 functions.php 文件中,并调用 is_tree(‘id’) 以查看当前页面是否是该页面或是否为该页面的子页面。在 Snippet 3 中,is_tree(‘2’) 将替换第一个 if 标签内的"is_page( ‘about’ ) || '2' == $post->post_parent"。

请注意,如果您有多个级别的页面,父页面是直接上方的那个,而不是层级顶部的最上面的那个。

是否是页面模板

允许您确定是否处于页面模板中或正在使用特定的页面模板。

is_page_template()

是否在使用页面模板?

is_page_template( 'about.php' );

是否正在使用名为‘about’的页面模板?注意,与其他条件不同,如果您想指定特定的页面模板,您需要使用文件名,例如 about.php 或 my_page_template.php。

注意:如果文件在子目录中,您必须包含此内容。这意味着这应该是相对于您的主题的文件路径以及文件名,例如'page-templates/about.php'。

分类页

is_category()

当显示分类归档页面时。

is_category( '9' );

当显示第 9 个类别的归档页面时。

is_category( 'Stinky Cheeses' );

当显示名为“Stinky Cheeses"的类别的归档页面时。

is_category( 'blue-cheese' );

当显示分类名称(slug)为“blue-cheese"的归档页面时。

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 个类别中,则返回 true。注意开头的感叹号!。

注意:测试时请确保拼写正确。“is”和"in"之间有很大的区别。

另见 is_archive() 和 分类模板。

标签页

is_tag()

当显示任何标签归档页面时。

is_tag( 'mild' );

当显示名称(slug)为'mild'的标签的归档页面时。

is_tag( array( 'sharp', 'mild', 'extreme' ) );

当显示的标签归档的名称(slug)是“sharp"、“mild”或“extreme”之一时返回 true。

has_tag();

当前文章有标签。必须在循环中使用。

has_tag( 'mild' );

当当前帖子具有't mild'标签时。

has_tag( array( 'sharp', 'mild', 'extreme' ) );

当当前文章拥有数组中的任何标签之一时。

另见 is_archive() 和 标签模板。

分类法页

is_tax()

当显示任何分类法归档页面时。

is_tax( 'flavor' );

当显示 flavor 分类法的分类法归档页面时。

is_tax( 'flavor', 'mild');

当显示名称(slug)为'mild'的 flavor 分类法的归档页面时。

is_tax( 'flavor', array( 'sharp', 'mild', 'extreme' ) );

当显示的 flavor 分类法归档的名称(slug)是“sharp"、“mild”或“extreme"之一时返回 true。

has_term()

检查当前文章是否拥有任何给定的术语。第一个参数应该是空字符串。它期望第二个参数为分类法名称(slug)。

has_term( 'green', 'color' );

当当前帖子具有来自'taxonomy color'的'term green'时。

has_term( array( 'green', 'orange', 'blue' ), 'color' );

当当前文章拥有数组中的任何术语之一时。

另见 is_archive()。

已注册的分类法

taxonomy_exists()

当通过register_taxonomy()注册特定分类法时。以前是 is_taxonomy(),已在 3.0 版本中弃用。

作者页

is_author()

当显示任何作者页面时。

is_author( '4' );

当显示第 4 个作者的归档页面(ID)时。

is_author( 'Vivian' );

当显示昵称为“Vivian"的作者的归档页面时。

is_author( 'john-jones' );

当显示用户名为"john-jones"的作者归档页面时。

is_author( array( 4, 'john-jones', 'Vivian' ) );

当显示的作者归档是用户 ID 为 4,或用户昵称为“john-jones",或昵称“Vivian”时。

另见 is_archive() 和 作者模板。

多作者网站

is_multi_author()

当有多个作者在网站上发布文章时。版本 3.2 可用。

日期页

is_date()

当显示任何基于日期的归档页面时(即按月、年、每日或时间为基础的归档)。

is_year()

当显示年度归档时。

is_month()

当显示月度归档时。

is_day()

当显示每日归档时。

is_time()

当显示每小时、每分钟或每秒归档时。

is_new_day()

如果根据文章日期,今天是新的一天。应在循环中使用。

任何归档页

is_archive()

当显示任何类型的归档页面时。分类、标签、作者和日期为基础的页面都是归档类型。

搜索结果页

is_search()

当显示搜索结果的归档页面时。

404 未找到页

is_404()

当页面在发生“HTTP 404:未找到”错误后显示时。

隐私政策页

is_privacy_policy()

当显示隐私策略页面时。

附件

is_attachment()

当显示文章或页面的附件文档时。附件是通过帖子编辑器的上传工具上传的图像或其他文件。附件可以单独在其自己的“页面”或模板上显示。

单个页面、单篇文章或附件

is_singular()

当以下任何一项返回 true 时:is_single(),is_page() 或 is_attachment()。

is_singular( 'book' );

查看自定义文章类型 book 的文章时为真。

is_singular( array( 'newspaper', 'book' ) );

当查看名为 newspaper 或 book 的自定义文章类型的帖子时返回 true。

聚合(Syndication)

is_feed()

当请求的网站是聚合时。此标签通常不由用户使用;它由 WordPress 内部使用,供插件开发人员使用。

回显(Trackback)

is_trackback()

当请求的网站是 WordPress 对其 Trackback 引擎的钩子时。此标签通常不由用户使用;它由 WordPress 内部使用,供插件开发人员使用。

预览

is_preview()

当显示的单个文章以草稿模式查看时。

有摘要(Excerpt)

has_excerpt()

当当前文章具有摘要时。

has_excerpt( 42 )

当第 42(ID)篇文章有摘要时。

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

已分配导航菜单

has_nav_menu()

注册的导航菜单位置是否已分配了菜单。

返回:assigned(true) 或 not(false)

在循环中

in_the_loop()

检查您是否“在循环内”。这对插件作者很有用,此条件在您处于循环内部时返回 true。

侧边栏是否激活

is_active_sidebar()

检查给定的侧边栏是否处于活动状态(使用中)。如果侧边栏(通过名称、ID 或数字标识)正在使用,则返回 true,否则函数返回 false。

网络(多站点)的一部分

is_multisite()

检查当前网站是否在 WordPress 多站点安装中。

主站(多站点)

is_main_site()

确定网站是否是网络中的主站。

网络的管理人员(多站点)

is_super_admin()

确定用户是否是网络(超级)管理员。

已激活的插件

is_plugin_active()

检查插件是否已激活。

子主题

is_child_theme()

检查是否正在使用子主题。

主题支持功能

current_theme_supports()

检查各种主题功能是否存在。

工作示例

以下是演示如何使用这些条件标签的工作样本。

单篇文章

此示例显示如何使用 is_single() 仅在查看单个文章页面时显示特定内容:

<?php
if ( is_single() ) {
	echo 'This is just one of many fabulous entries in the ' . single_cat_title() . ' category!';
}

另一个在循环中使用条件标签的示例。选择在此处显示内容或摘要,当这是单篇文章页面或主页时。

<?php
if ( is_home() || is_single() ) {
	the_content();
} else {
	the_excerpt();
}

当您需要在不是主页的地方显示代码或元素时。

<?php if ( ! is_home() ) {
	// Insert your markup ...
}

检查多个条件

您可以使用 PHP 运算符在单个 if 语句中评估多个条件。

如果您需要检查条件组合是否评估为 true 或 false,这很方便。

<?php

// 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.
}
<?php

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

<?php endwhile; endif; ?>

可变侧边栏内容

此示例将根据读者当前查看的页面在您的侧边栏中显示不同的内容。

<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.';
}
?>

</div><!-- #sidebar -->

有用的 404 页

创建错误 404 页面文章作为在编写友好消息部分中使用 PHP 条件函数 isset()的示例。

在主题的 footer.php 文件中

有时在其他模板(如 sidebar.php)中执行的查询可能会破坏某些条件标签。例如,在 header.php 中的条件标签可以正常工作,但在主题的 footer.php 中不起作用。诀窍是在 footer 的条件测试之前放置 wp_reset_query。例如:

<?php
wp_reset_query();
if ( is_page( '2' ) ) {
	echo 'This is page 2!';
}

条件标签索引

条件标签列表

函数参考

参考