Get the ID of the Current or Parent Category in the Template category.php

When developing a WordPress theme, you may encounter the need to display the ID of the current category or its parent category in a category template – category.php.

This can be achieved using the following code snippet:

$category = get_queried_object();
$parent_category_id = $category->parent;
$current_category_id = $category->term_id;

if ($category) {
    echo 'Current category ID: ' . $current_category_id;
    echo 'Parent category ID: ' . $parent_category_id;

This code retrieves the current category and its parent category using the get_queried_object() function, and then displays their ID’s using the echo statement.

This code snippet can be added to your category template to display the ID’s of the current and parent categories.