Skip to content

How to get current category name when having only the product ID

I want to get the current category name in a .phtml file, having only the product ID.
The product ID is fetched from reviews.

Note: I’m not running this on product page.

This is what I have now:

<?php
$objectManager = MagentoFrameworkAppObjectManager::getInstance();
$reviewData = $block->getAllRatingData();

foreach ($reviewData as $reviewKey => $reviewValue) {

$productId   = $reviewValue['entity_pk_value'];
$product     = $objectManager->create('MagentoCatalogModelProduct')->load($productId);
$productName = $product->getName();
$productUrl  = $product->getProductUrl();

?>
<div>
    <span><?php echo $reviewValue['Grade']; ?></span><br>
    <span><a href="<?php echo($productUrl); ?>"><?php echo($productName); ?></a></span><br>
    <strong><?php echo $reviewValue['title']; ?></strong><br>
    <span><?php echo date_format(date_create($reviewValue['created_at']),"Y-m-d"); ?> 
    <?php echo $reviewValue['nickname']; ?></span><br>
    <em><?php echo $reviewValue['detail']; ?></em>
</div>
<br>
<?php
}
?>

Thanks,