Quantcast
Channel: WPTaskforce » Category
Viewing all articles
Browse latest Browse all 6

How to get the category ID of the current post?

$
0
0

How to get the category ID of the current post? Just copy the codes below and paste it into your .php file.

$category = get_the_category( get_the_ID() );
$category_ID = $category[0]->term_id;

Another problem is, what if the category ID is a sub category and you need the parent category ID? For Example, the current category ID is 2 and this is a sub category of 1 ( which is 1 is the category you are looking ), then you need codes below.

$category = get_the_category( get_the_ID() );
$category_ID = $category[0]->term_id;
$parent_id = get_ancestors( $category_ID, 'category' ); 
if( in_array( 1, $parent_id ) ){
// codes if true
} 

The post How to get the category ID of the current post? appeared first on WPTaskforce.


Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images