Try below code in vendor/magento/module-checkout/view/frontend/templates/cart/form.phtml
<?php if ($block->getContinueShoppingUrl()) :?>
<?php
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$checkoutSession = $objectManager->get('Magento\Checkout\Model\Session');
$items = $checkoutSession->getQuote()->getAllItems();
$max = 0;
$lastItem = null;
foreach ($items as $item){
if ($item->getId() > $max) {
$max = $item->getId();
$lastItem = $item;
}
}
if ($lastItem) {
$lastCategory = $lastItem->getCategoryCollection()
->addAttributeToSelect('*')
->addOrder('level');
}
if ($lastCategory) {
$categoryUrl = $lastCategory->getFirstItem()->getUrl();
}
if ($categoryUrl) {
?>
<a class="action continue"
href="<?= $block->escapeUrl($categoryUrl) ?>"
title="<?= $block->escapeHtmlAttr(__('Continue Shopping')) ?>">
<span><?= $block->escapeHtml(__('Continue Shopping')) ?></span>
</a>
<?php
} else {
?>
<a class="action continue"
href="<?= $block->escapeUrl($block->getContinueShoppingUrl()) ?>"
title="<?= $block->escapeHtmlAttr(__('Continue Shopping')) ?>">
<span><?= $block->escapeHtml(__('Continue Shopping')) ?></span>
</a>
<?php
}
?>
<?php endif; ?>
NOTE: Replace object manager with collections as it is not recommended in Magento