通知短信+运营短信,5秒速达,支持群发助手一键发送🚀高效触达和通知客户 广告
根据当前页面匹配的条件,可以在模板文件中使用条件标签来更改内容的显示。 他们告诉WordPress在特定条件下显示什么代码。 条件标签通常使用PHP if / else条件语句。 代码首先检查一个语句是真还是假。 如果发现该语句为true,则执行第一组代码。 如果它是假的,则跳过第一组代码,而代替执行第二组代码(在else之后)。 ``` if ( is_user_logged_in() ): echo 'Welcome, registered user!'; else: echo 'Welcome, visitor!'; endif; ``` >[warning] 请注意这些标签与WordPress模板层次结构的密切关系。 [被添加或删除?] ## 使用条件标签的位置 对于要修改数据的条件标签,信息必须已从数据库检索,即查询必须已经运行。 如果在有数据之前使用条件标签,那么就没有什么可以询问if / else语句。 重要的是要注意,在运行查询之前,WordPress加载了functions.php,所以如果您只是在该文件中包含一个条件标签,它将无法正常工作。 两种实现条件标签的方式: - 将其放在模板文件中 - 在functions.php中创建一个函数,该函数挂钩到稍后触发的action/filter # 条件 以下列出了以下条件,其中以下每个条件语句证明是真实的。 注意可以接受参数的标签。 ## 主页 - is_home() 当显示主博客页面时,此条件返回true,通常以标准的反向时间顺序排列。 如果您的主页已经设置为静态页面,那么这只会在您设置为“设置>阅读”中设置为“帖子”页面的页面中证明是真实的。 ## 首页 - is_front_page() 当站点的首页显示时,无论是否设置为显示帖子或静态页面,此条件都将返回true。 在以下情况下返回true: 正在显示主要博客页面 设置>阅读 - >首页显示选项设置为您的最新帖子 要么 当设置>阅读 - >首页显示设置为静态页面和 前页值是当前页面显示。 ## 管理页 - is_admin() 当显示仪表板或管理面板时,此条件返回true。 ## 单页面 - is_single() 当显示任何单个帖子(或附件或自定义帖子类型)时,返回true。 如果您在页面上,此条件返回false。 - is_single( ’17’ ) is_single() 还可以通过ID和其他参数检查某些帖子。 当Post 17显示为单个帖子时,上述示例证明是正确的。 - is_single( ‘Irish Stew’ ) 参数也包括Post标题。 在这种情况下,当标题为“爱尔兰炖肉”的帖子被显示为单个帖子时,这是真实的。 - is_single( ‘beef-stew’ ) 当邮政邮局“牛炖肉”被显示为单一邮政时,证明是正确的。 - is_single( array( 17, ‘beef-stew’, ‘Irish Stew’ ) ) 当显示的单个帖子是帖子ID 17或post_name是“牛肉炖”,或post_title是“爱尔兰炖肉”时,返回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。 >[warning] 注意:此功能不区分帖子ID,帖子标题或帖子名称。 如果请求17的职位ID,将显示一个名为“17”的帖子。 大概同样适用于带有“the”的帖子。 ## 单个帖子,页面或附件 - is_singular() 对于任何is_single,is_page和is_attachment返回true。 它允许测试post类型。 ## 置顶帖子 - is_sticky() 如果当前帖子中已经选中了“置顶帖子”复选框,则返回true。 在此示例中,没有发布帖子ID参数,因此使用循环帖子的帖子ID。 - is_sticky( ’17’ ) Post 17被认为是置顶帖子的帖子时返回true。 ## 一个帖子类型 - get_post_type() 您可以通过在您的条件中包含get_post_type()来测试当前帖子是否为某种类型。 它不是一个条件标签,但它返回当前帖子的注册的帖子类型。 ``` if ( ‘book’ == get_post_type() ) … ``` - post_type_exists() 如果给定的帖子类型是注册的帖子类型,则返回true。 这不会测试一个帖子是否是某个post_type。 >[warning] 注意:此函数在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的post类型归档页面(可以是单个帖子类型或Post类型数组),则返回true。 要打开帖子类型的档案,请在注册帖子类型时使用'has_archive'=> true。 ## 评论弹出窗口 - is_comments_popup() 当在评论弹出窗口。 ## 任何页面包含帖子 - comments_open() 当WordPress循环中正在处理当前帖子的注释时。 - pings_open() 当允许在WordPress循环中处理当前帖子的ping时。 ## “PAGE”页面 本节涉及WordPress页面,不是您博客中的任何通用网页,也不是内置的post_type“页面”。 - is_page() 何时显示任何页面。 - is_page( ’42’ ) 当显示Page 42(ID)时。 - is_page( ‘About Me And Joe’ ) 当显示带有“About Me And Joe”的post_title的页面时。 - is_page( ‘about-me’ ) 当显示带有“about-me”的post_name(slug)的页面时。 - 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 = 54或帖子ID = 6时,返回true。 ## 分页的测试 您可以使用此代码来检查您是否位于使用该页面的页面中的第n页`<!--nextpage--\>` QuickTag 这可能是有用的,例如,如果您希望仅在分为几页的帖子的第一页上显示元数据。 **示例 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 } ?> ``` 您可以使用Snippet 2中的代码创建自己的is_subpage()函数。将其添加到functions.php文件中。 它以与Snippet 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 } } ``` 建议在Snippet 2中使用这样的功能,而不是像Snippet 1那样使用简单的测试,如果您打算经常测试子页面。 要测试页面的父级是否是特定页面,例如“关于”(默认为页面ID为2),我们可以在Snippet 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 } ?> ``` Snippet 4是一个允许您更容易地进行上述测试的功能。 如果我们查看有问题的页面(如“about”)或其一个子页面(因此,具有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 } ``` 将Snippet 4添加到您的functions.php文件中,并调用is_tree('id')来查看当前页面是页面还是页面的子页面。 在代码段3中,is_tree('2')将替换“is_page('about')|| '2'== $ post-> post_parent“里面的第一个if标签。 >[warning] 请注意,如果您有多个级别的页面,则父页面是上一页,而不是层次结构顶层的页面。 ## 是页面模板 允许您确定是否在页面模板中,或者是否正在使用特定的页面模板。 - is_page_template() 是否使用页面模板? - is_page_template( ‘about.php’ ) 是否使用页面模板? 请注意,与其他条件不同,如果要指定特定的页面模板,则需要使用文件名,如about.php或my_page_template.php。 >[warning] 注意:如果文件位于子目录中,那么您也必须包含此目录。 这意味着这应该是与您的主题相关的文件路径以及文件名,例如“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或者“Stinky Cheeses”或“blue-cheese”时,返回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。注意! 一开始 >[warning] 注意:测试时请务必检查拼写。 “is”或“in”之间有很大的区别。 另请参见is_archive()和类别模板。 ## 标签页 - is_tag() 当任何标签存档页面被显示时。 - is_tag( ‘mild’ ) 当显示带有“mild”块的标签的存档页面时。 - is_tag( array( ‘sharp’, ‘mild’, ‘extreme’ ) ) 正在显示的标签存档显示为“sharp”,“mild”或“extreme”时,返回true。 - has_tag() 当前帖子有标签。 必须在循环中使用。 - has_tag( ‘mild’ ) 当前帖子标签为“mild”时。 - has_tag( array( ‘sharp’, ‘mild’, ‘extreme’ ) ) 当前帖子在数组中有任何标签时。 另请参见is_archive()和标签模板。 ## 分类页 - is_tax() 当显示任何分类归档页面时。 - is_tax( ‘flavor’ ) 当显示'flavor'分类的分类标准页面时。 - is_tax( ‘flavor’, ‘mild’) 当显示具有“flavor”和“mild”分类法的归档页面时。 - is_tax( ‘flavor’, array( ‘sharp’, ‘mild’, ‘extreme’ ) ) 当显示的'flavor'分类存档显示为“sharp”,“mild或“extreme”时,返回true。 - has_term() 检查当前帖子是否具有任何特定条款。 第一个参数应该是一个空字符串。 它预期分类标语/名称作为第二个参数。 - has_term( ‘green’, ‘color’ ) 当前帖子从分类“color”中有“green”一词。 - has_term( array( ‘green’, ‘orange’, ‘blue’ ), ‘color’ ) 当前职位在阵列中有任何术语。 另请参见is_archive()。 ## 注册分类 - taxonomy_exists() 当通过register_taxonomy()注册一个特定的分类法时。 以前的is_taxonomy(),在版本3.0中已被弃用 ## 作者页 - is_author() 当任何作者页面被显示时。 - is_author( ‘4’ ) 当作者号(ID)4的存档页面正在显示时。 - is_author( ‘Vivian’ ) 当昵称为“Vivian”的作者的存档页面被显示时。 - is_author( ‘john-jones’ ) 当显示具有Nicename“john-jones”的作者的归档页面时。 - is_author( array( 4, ‘john-jones’, ‘Vivian’ ) ) 当作者的存档页面是用户ID 4或user_nicename“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_attachment() 附件文件显示在帖子或页面时。 附件是通过帖子编辑器的上传实用程序上传的图像或其他文件。 附件可以显示在自己的“页面”或模板上。 ## 单页,单张或附件 - is_singular() 当以下任何一个返回true时:is_single(),is_page()或is_attachment()。 - is_singular( ‘book’ ) 查看“自定义帖子类型”图书的帖子时为真。 - is_singular( array( ‘newspaper’, ‘book’ ) ) 当查看自定义帖子类型的报纸或书籍的帖子时是真的。 ## 一个联合 - is_feed() 当网站要求是一个联合会。 该标签通常不被用户使用; 它由WordPress内部使用,可用于插件开发人员。 ## 回溯 - is_trackback() 当请求的站点是WordPress钩入其Trackback引擎。 该标签通常不被用户使用; 它由WordPress内部使用,可用于插件开发人员。 ## 预览 - is_preview() 当在草稿模式下查看显示的单个帖子。 ## 有摘录 - 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() ) { // you text or code } ?> ``` ## 已分配导航菜单 - has_nav_menu() 注册的导航菜单位置是否已分配菜单 返回:assign(true)或not(false) ## 内循环 - in_the_loop() 检查你是否在“循环内”。 对于插件作者很有用,当您在循环中时,此条件返回为真。 ## 边栏活跃 - is_active_sidebar() 检查给定侧边栏是否处于活动状态(正在使用中)。 如果边栏(由name,id或number标识)正在使用,则返回true,否则函数返回false。 ## 网络的一部分(多站点) - is_multisite() 检查当前站点是否在WordPress MultiSite安装中。 ## 主站(多站点) - is_main_site() 确定站点是否是网络中的主站点。 ## 网络管理员(多站点) - is_super_admin() 确定用户是否是网络(超级)管理员。 ## 一个活动插件 - is_plugin_active() 检查插件是否被激活。 ## 子主题 - is_child_theme() 检查子主题是否正在使用。 ## 主题支持功能 - current_theme_supports() 检查是否存在各种主题功能。 # 工作实例 以下是演示如何使用这些条件标签的工作示例。 ## 单张帖子 此示例显示如何使用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 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 } <h3>Date Based Differences</h3> 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 title="Permanent Link to <?php the_title(); ?>" 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> ``` ## 有用的404页 创建错误404页面文章[NEED Link to this?]有一个在“写入友好的消息”部分中使用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!'; } ?> ``` ## 条件标签索引 ### 评论开放 - has_tag - has_term - in_category - is_404 - is_admin - is_archive - is_attachment - is_author - is_category - is_child_theme - is_comments_popup - is_date - is_day - is_feed - is_front_page - is_home - is_month - is_multi_author - is_multisite - is_main_site - is_page - is_page_template - is_paged - is_preview - is_rtl - is_search - is_single - is_singular - is_sticky - is_super_admin - is_tag - is_tax - is_time - is_trackback - is_year - pings_open ## 功能参考 - Function: comments_open() - Function: is_404() - Function: is_admin() - Function: is_admin_bar_showing() - Function: is_archive() - Function: is_attachment() - Function: is_author() - Function: is_category() - Function: is_comments_popup() - Function: is_date() - Function: is_day() - Function: is_feed() - Function: is_front_page() - Function: is_home() - Function: is_local_attachment() - Function: is_main_query - Function: is_multi_author - Function: is_month() - Function: is_new_day() - Function: is_page() - Function: is_page_template() - Function: is_paged() - Function: is_plugin_active() - Function: is_plugin_active_for_network() - Function: is_plugin_inactive() - Function: is_plugin_page() - Function: is_post_type_archive() - Function: is_preview() - Function: is_search() - Function: is_single() - Function: is_singular() - Function: is_sticky() - Function: is_tag() - Function: is_tax() - Function: is_taxonomy_hierarchical() - Function: is_time() - Function: is_trackback() - Function: is_year() - Function: in_category() - Function: in_the_loop() - Function: is_active_sidebar() - Function: is_active_widget() - Function: is_blog_installed() - Function: is_rtl() - Function: is_dynamic_sidebar() - Function: is_user_logged_in() - Function: has_excerpt() - Function: has_post_thumbnail() - Function: has_tag() - Function: pings_open() - Function: email exists() - Function: post_type_exists() - Function: taxonomy_exists() - Function: term_exists() - Function: username exists() - Function: wp_attachment_is_image() - Function: wp_script_is()