get the category wp

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

// Step 2: Get the category IDs associated with the post
$category_ids = wp_get_post_categories($post_id);

// Step 3: Check if categories are available
if ($category_ids) {
    // Step 4: Loop through each category
    foreach ($category_ids as $category_id) {
        // Step 5: Get the category details
        $category = get_category($category_id);

        // Step 6: Display category name and link
        echo '<a href="' . esc_url(get_category_link($category_id)) . '">' . esc_html($category->name) . '</a>';
    }
} else {
    // Step 7: Display a message if no categories are found
    echo 'No categories found.';
}
?>