Check if current page is category page in wordpres

2020-07-06 05:22发布

问题:

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

回答1:

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.



回答2:

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


回答3:

You can use

is_category();

function.

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



回答4:

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 }


标签: php wordpress