8

I'm trying to get category ID of the current archive displayed.

I tried:

// category (can be a parent category)
$current_cat_ID = get_query_var('cat');
//
print_r ($current_cat_ID);

It doesn't print anything...

  • where in the template is the code located? are there any custom queries before the code? – Michael Jul 24 '12 at 14:21
  • will cat always be set regardless of permalink structure, or maybe just category_name in some cases? I can't recall at the moment... – Milo Jul 24 '12 at 14:29
26
+50

you can use get_queried_object()

$category = get_queried_object();
echo $category->term_id;
  • 3
    I've been searching for an answer to his for hours. I wish Wordpress had better documentation. Thank you! – Django Reinhardt Feb 24 '17 at 15:27
  • @Fayaz It will automatically apply to this answer. – Django Reinhardt Mar 1 '17 at 11:02
1

If you want to fecth in category page,you can get id of current category by:

$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;

If you want to get category id of any particular category on any page, you can try using below code:

$category_id = get_cat_ID('Category Name');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy