Biz Connection FREE TRIAL + $75 GOOGLE ADWORD - FREE VOUCHER for YOU!

Category ID – WordPress

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');
}

Related articles

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.00 out of 5)
Loading ... Loading ...

Leave a Comment