Home » WordPress Tutorials » How to Get Category ID of Current Category

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.

By

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 WPsite.net (this awesome site!).

Twitter Facebook Web Site

2 Responses to How to Get Category ID of Current Category

  1. Pingback: Using WP Category Posts List Widget To Dynamically Display Specific Category (WP/PHP) - feed99

  2. anonim Reply

    July 4, 2013 at 3:21 AM

    Thanksss!!!! veryyy very thanks !!!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>