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.

WPsite.net

WPsite.net will feature articles on WordPress (themes, plugins, and tutorials, Internet Marketing, Blogging, SEO, Web Design, and Social Media

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 *

Get 25% off Plugins!

Receive a 25% discount code for DraftPress plugins when you sign up to the newsletter!

We promise never to spam or share your information.
Powered by Popup Fire