get current post categody id wp

<?php
// Step 1: Get the current post ID
$current_post_id = get_the_ID();

// Step 2: Get the categories assigned to the current post
$post_categories = get_the_category($current_post_id);

// Step 3: Check if there are categories assigned to the post
if (!empty($post_categories)) {
    // Step 4: Get the ID of the first category assigned to the post
    $first_category_id = $post_categories[0]->cat_ID;

    // Step 5: Output or use the first category ID as needed
    echo $first_category_id;
} else {
    // Step 6: Handle the case where no categories are assigned to the post
    echo 'No categories assigned to the current post.';
}
?>