J
J
jww2017-04-05 20:10:50
PHP
jww, 2017-04-05 20:10:50

How to get the category id of the current post?

How to get the category id of the current post? The below function does not work, I use it in a while loop.
$category = get_the_category(the_ID());

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alvvi, 2017-04-05
@jww

$category = get_the_category(get_the_ID());
$cat_id = get_cat_ID($category[0]->cat_name);
echo $cat_id;

Why do you need these extra steps?
$categories = get_the_category(); // Внутри цикла
$cat_id = $categories[0]->cat_ID // ID самой категории
$parent_id = $categories[0]->category_parent // ID категории-родителя, если нужен

L
laz_art, 2020-02-04
@laz_art

get_the_category($post->ID)[0]->term_id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question