How to Get Category ID of Current Category

Yesterday, I was looking for ways to grab the current category ID so that I could display a custom image at the top of each archive page. While there are many different ways to achieve this, I did not want to run an additional query. After reviewing the solutions mostly, I stumbled upon this gem on the WordPress Codex:

<?php get_query_var( $var ); ?>

With that template tag, you can simply pass the variable cat to grab the Category ID of the current category (i.e., within archive.php):

<?php echo get_query_var('cat'); ?>

You can use it to grab the Page ID of the current page as well (i.e., within page.php):

<?php echo get_query_var('page'); ?>

That snippet will accept any parameter that can be used within the WP Query class.


Charlie Patel is a world traveler, WordPress geek, serial entrepreneur, & consultant to big-shots in the corporate world and on the web. Otherwise, he's just planning the next trip or in some far-away land. He runs (this awesome site!).

2 thoughts on “How to Get Category ID of Current Category

Leave a Reply

Your email address will not be published. Required fields are marked *