Check if current page is category page in wordpres

2020-07-06 05:27发布

I have to insert some condition if the current page is a category page or not.

标签: php wordpress
4条回答
Animai°情兽
2楼-- · 2020-07-06 06:12

I have found the way to do it by checking if $cat_id is available or not on that page by the following.

$cat_id = get_query_var('cat');

Now we can check if $cat_id is available then it is a category page otherwise it is not.

查看更多
Bombasti
3楼-- · 2020-07-06 06:22

Amit's answer will work but there is a simpler way as Wordpress sets a Global var for the category id of $cat. So just checking if this is set will tell you if you are on a category page.

if(!empty($cat)){ //do something just on a category archive page }
查看更多
ら.Afraid
4楼-- · 2020-07-06 06:25

you can use this for getting category

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
查看更多
啃猪蹄的小仙女
5楼-- · 2020-07-06 06:26

You can use

is_category();

function.

refrence: http://codex.wordpress.org/Function_Reference/is_category

查看更多
登录 后发表回答