[php] Get current category ID of the active page

Looking to pull the category ID of a specific page in WordPress that is listing all posts using that specific category. Tried the below but not working. I am able to get the category name using single_term_title.

$category = single_term_title("", false);
$catid = get_cat_ID( $category );

$category is displaying "Entertainment" for example. But I also need the ID of "Entertainment". How would I go about this?

This question is related to php wordpress wordpress-theming categories

The answer is


Tried above for solutions to find cat ID of a post, but nothing worked, used the following instead:

$obj = get_queried_object();
$c_id = wp_get_post_categories($obj->ID);

Alternative -

 $catID = the_category_ID($echo=false);

EDIT: Above function is deprecated please use get_the_category()


I used this for breadcrums in the category template page:

$cat_obj = $wp_query->get_queried_object();
$thiscat_id = $cat_obj->term_id;
$thiscat = get_category($thiscat_id);
$parentcat = get_category($thiscat->parent);

I found this question whilst looking for exactly what you asked. Unfortunately you have accepted an incorrect answer. For the sake of other people who are trying to achieve what we were trying to achieve, I thought I'd post the correct answer.

$cur_cat = get_cat_ID( single_cat_title("",false) );

As you said single_term_title("", false); was correctly returning the category title, I'm not sure why you would have had troubles with your code; but the above code works flawlessly for me.


If it is a 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, try using :

$category_id = get_cat_ID('Category Name');

The oldest but fastest way you can use is:

$cat_id = get_query_var('cat');

I use the get_queried_object function to get the current category on a category.php template page.

$current_category = get_queried_object();

Jordan Eldredge is right, get_the_category is not suitable here.


I think some of the above may work but using the get_the_category function seems tricky and may give unexpected results.

I think the most direct and simple way to access the cat ID in a category page is:

$wp_query->query_vars['cat']

Cheers


$cats = wp_get_post_terms( $post->ID, 'product_cat' );
foreach($cats as $cat){
/*check for category having parent or not except category id=1 which is wordpress default category (Uncategorized)*/
  if($cat->parent != '0' && $cat->term_id != 1){
    echo '<h2 class="link"><a href="'.get_category_link($cat->term_id ).'">'.$cat->name.'</a></h2>';
    break;
  }
}

if you need the category ID, you would get it via get_query_var, that is capable of retrieving all publicly queryble variables.

$category_id = get_query_var('cat');

here is an example to get the category name

$category_name = get_query_var('category_name');

and of course the all mighty get_queried_object

$queried_object = get_queried_object();

that is returning the complete taxonomy term object (when used on a taxonomy-archive page..)


Examples related to php

I am receiving warning in Facebook Application using PHP SDK Pass PDO prepared statement to variables Parse error: syntax error, unexpected [ Preg_match backtrack error Removing "http://" from a string How do I hide the PHP explode delimiter from submitted form results? Problems with installation of Google App Engine SDK for php in OS X Laravel 4 with Sentry 2 add user to a group on Registration php & mysql query not echoing in html with tags? How do I show a message in the foreach loop?

Examples related to wordpress

#1273 – Unknown collation: ‘utf8mb4_unicode_520_ci’ How to get WooCommerce order details Wordpress plugin install: Could not create directory WooCommerce: Finding the products in database How to get post slug from post in WordPress? How to get featured image of a product in woocommerce Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\wordpress\wp-includes\class-http.php on line 1610 Use .htaccess to redirect HTTP to HTTPs Load More Posts Ajax Button in WordPress How to decode encrypted wordpress admin password?

Examples related to wordpress-theming

The requested URL /about was not found on this server Get current category ID of the active page Redirect after Login on WordPress WordPress path url in js script file How can I get the current page name in WordPress? Get Wordpress Category from Single Post

Examples related to categories

Remove category & tag base from WordPress url - without a plugin Get Category name from Post ID Get current category ID of the active page How to get the category title in a post in Wordpress?