WordPress is jumping as usual from one version to another and many rock functions become deprecated.
As WordPress developer you may need current functions.
Get current categoryID if you know the post ID. That’s simple and common and there is a function which return all you wish about that category:
The function is :
get_the_category($post->ID);
For full info check WordPress “Functions references”.
Example:
<?php global $post; $categories = get_the_category($post->ID); var_dump($categories); ?>
What’s happening if you just wish to capture category ID if you list all posts inside a category (archive). That’s the point where functions were deprecated. There are a few options if you consider starting from a Category name or category slung but you may change them and your coding become unusefull – errors.
The single way is to use
get_query_var('cat');
For full info check WordPress “Functions references”.
Example:
if(is_category() || is_single()){
$cat_ID = get_query_var('cat');
}



(2 votes, average: 4.00 out of 5)
(20 votes, average: 4.75 out of 5, rated)