15

如果当前页面是类别页面,我必须插入一些条件。

4

4 回答 4

15

您可以使用它来获取类别

is_category();
is_category( '1' ); // where 1 is category id
is_category( 'test' ); // where test is category name
is_category( 'test-ing' ); // where test-ing is category slug
于 2013-10-24T07:05:47.907 回答
12

我通过以下方式检查$cat_id该页面上是否可用,找到了方法。

$cat_id = get_query_var('cat');

现在我们可以检查 $cat_id 是否可用,那么它是一个类别页面,否则它不是。

于 2013-10-24T06:29:15.137 回答
7

您可以使用

is_category();

功能。

参考:http : //codex.wordpress.org/Function_Reference/is_category

于 2013-10-24T06:35:32.653 回答
2

Amit 的答案会起作用,但有一种更简单的方法,因为 Wordpress 为 $cat 的类别 id 设置了一个全局变量。因此,只需检查是否设置了它就会告诉您您是否在类别页面上。

if(!empty($cat)){ //do something just on a category archive page }
于 2017-08-08T13:52:09.537 回答