主题开发文档

条件标签列表

💡 云策文档标注

概述

条件标签是布尔数据类型,用于在模板文件中根据当前页面匹配的条件来调整内容显示。它们通常与 PHP if/else 条件语句结合使用,并与 WordPress 模板层次结构密切相关。

关键要点

  • 条件标签返回 TRUE 或 FALSE,用于控制特定条件下的代码执行。
  • 必须在 WP_Query 设置后或使用 action hook 时才能使用条件查询标签。
  • 文档提供了完整的条件标签列表,包括 is_home()、is_single()、is_page() 等,并分类说明了它们在不同页面类型(如首页、文章页、分类页)中的适用条件。

注意事项

  • 使用条件标签时需注意其依赖关系,避免在 WP_Query 未初始化前调用。
  • 部分标签可接受参数(如 is_category($category)),需根据文档正确使用。

📄 原文内容

Conditional Tags are a boolean data type that can be used in your Template Files to alter the display of content depending on the conditions that the current page matches. They tell WordPress what code to display under specific conditions. Conditional Tags usually work with PHP if /else Conditional Statements and have close relation with WOrdPress Template Hierarchy. .

Warning: You can only use conditional query tags after the WP_Query is set up or  with action hook.

Complete List of Conditional Tags

The Conditions For …

All of the Conditional Tags test to see whether a certain condition is met, and then returns either TRUE or FALSE. The conditions under which various tags output TRUE is listed below. Those tags which can accept parameters are so noted.

The Main Page

The Front Page

The Blog Page

A Single Post Page

A PAGE Page

Has Post Thumbnail

A Single Page, a Single Post, an Attachment or Any Other Custom Post Type

A Category Page

A Tag Page

A Taxonomy Page (and related)

An Author Page

A Date Page

Any Archive Page

A Search Result Page

A 404 Not Found Page

Is Dynamic SideBar

Is Sidebar Active

Is Widget Active

Is User Logged in

Email Exists

Username Exists

A Paged Page

Right To Left Reading

An Attachment

Attachment Is Image

A Local Attachment

Post Type Exists

Is Main Query

A New Day

A Syndication

A Trackback

A Preview

Has An Excerpt

Has A Nav Menu Assigned

Is Blog Installed

Part of a Network (Multisite)

An Active Plugin

A Child Theme

Theme supports a feature

Is Previewed in the Customizer