一、WordPress 通用条件函数
这是 WordPress 核心自带的,和 WooCommerce 没直接关系:
is_home()
判断是否为博客首页(文章列表页)。is_front_page()
判断是否为网站首页(可能是静态页面)。is_single()
判断是否为单篇文章(post)。is_page()
判断是否为单个页面(page)。is_post_type_archive( $post_type )
判断是否为某个自定义文章类型的归档页。is_category( $slug )
判断是否为文章分类归档页。is_tag( $slug )
判断是否为标签归档页。is_tax( $taxonomy, $term )
判断是否为自定义分类法(taxonomy)的归档页。
二、WooCommerce 相关条件函数
WooCommerce 在 WordPress 的基础上扩展了很多条件函数:
is_shop()
判断当前页面是否为 商店页面(Shop Page)。is_product_category( $slug )
判断是否为某个 产品分类归档页(相当于is_tax( 'product_cat', $slug ))。is_product_tag( $slug )
判断是否为某个 产品标签归档页(相当于is_tax( 'product_tag', $slug ))。is_product()
判断是否为单个 产品详情页。is_cart()
判断是否为购物车页面。is_checkout()
判断是否为结账页面。is_account_page()
判断是否为“我的账户”页面。is_wc_endpoint_url( $endpoint )
判断 WooCommerce 的某个 账户/订单端点 URL,如/my-account/edit-address/。
三、区别说明
is_tax()与is_product_category()is_tax( 'product_cat' ):通用函数,判断自定义分类product_cat(产品分类)。is_product_category():WooCommerce 提供的简写,功能上等价,但语义更清晰。
is_product()与is_single()is_single():检查文章(post)是否单页,默认只作用于文章类型post。is_product():WooCommerce 特有,用于检查当前是否为单个商品页面,相当于is_singular( 'product' )。
is_post()
⚠️ 注意:WordPress 中并没有is_post()这个函数,一般用is_single()或is_singular( 'post' )来判断普通文章。
四、常见 WooCommerce 条件函数列表(便于参考)
is_shop()→ 商店页is_product_category()→ 产品分类页is_product_tag()→ 产品标签页is_product()→ 单个产品页is_cart()→ 购物车is_checkout()→ 结账页is_account_page()→ 我的账户is_wc_endpoint_url()→ WooCommerce 特定端点
👉 总结:
- WordPress 自带的
is_*系列(如is_single,is_page,is_tax)适用于通用场景。 - WooCommerce 扩展的
is_*系列(如is_product,is_cart,is_checkout)是对 WooCommerce 特定页面的快捷判断。
WordPress & WooCommerce 条件函数对照表
| 分类 | WordPress 条件函数 | WooCommerce 条件函数 | 说明 |
|---|---|---|---|
| 首页 | is_home() | – | 博客文章列表页 |
is_front_page() | – | 网站首页(可能是静态页) | |
| 单页/单篇 | is_single() | – | 普通文章(post)详情页 |
is_page() | – | 普通页面(page)详情页 | |
is_singular( 'post' ) | – | 单个文章页(更精确写法) | |
is_singular( 'product' ) | is_product() | WooCommerce 产品详情页 | |
| 归档页 | is_category() | – | 文章分类页 |
is_tag() | – | 文章标签页 | |
is_tax( 'taxonomy' ) | is_product_category() / is_product_tag() | 自定义分类法归档,WooCommerce 有简写函数 | |
is_post_type_archive( 'post_type' ) | is_shop() | 自定义文章类型归档,WooCommerce 的 product 归档就是 Shop 页 | |
| Woo 特殊页面 | – | is_cart() | 购物车页面 |
| – | is_checkout() | 结账页面 | |
| – | is_account_page() | 我的账户页面 | |
| – | is_wc_endpoint_url( $endpoint ) | WooCommerce 特定端点,例如 /my-account/orders/ | |
| 其他 | is_archive() | – | 任意归档页(含分类、标签、自定义分类、文章类型归档) |
is_search() | – | 搜索结果页 | |
is_404() | – | 404 页面 |
🔑 记忆技巧:
- WordPress 的条件函数以 通用类型为主,比如
is_single,is_page,is_tax。 - WooCommerce 的条件函数多为 产品和购物流程相关,比如
is_product,is_cart,is_checkout。 - 一般来说,
is_product_category()就是is_tax( 'product_cat' )的语法糖。