Skip to content

Get parent category url in Catalog Widget template

I have a cms page that I am using to display products via CatalogWidget template. I would like to modify the template to have the title of the category link to the category page, but I have been unsuccessful so far. Hopefully someone can point me in the right direction for this to work.

My CMS page has multiple product list widgets on the page, using the following widget code (with different category ids assigned).

    {{widget type="MagentoCatalogWidgetBlockProductProductsList" title="" show_pager="0" products_count="10" template="Magento_CatalogWidget::product/widget/content/chemlist.phtml" conditions_encoded="^[`1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Combine`,`aggregator`:`all`,`value`:`1`,`new_child`:``^],`1--1`:^[`type`:`Magento||CatalogWidget||Model||Rule||Condition||Product`,`attribute`:`category_ids`,`operator`:`==`,`value`:`1923`^]^]"}}

The chemlist.phtml file (referenced above) contains the following code at the top of the file ( I have omitted the foreach code here, since that code is working and I only need help getting the parent category url of the list).

    <?php

use MagentoFrameworkAppActionAction;

/** @var MagentoCatalogWidgetBlockProductProductsList $block */
?>
<?php if ($exist = ($block->getProductCollection() && $block->getProductCollection()->getSize())) : ?>



    <?php
    $type = 'widget-product-grid';
    $mode = 'list';
    $items = $block->getProductCollection()->getItems();
    $showCart = true;
    $templateType = MagentoCatalogBlockProductReviewRendererInterface::SHORT_VIEW;
    $description = false;
    ?>


<li class="list-group-item">
<hr style="background-color: #333; height: 2px; border: 0;" />
        <div class="row 2col chemList">
            
<div class="col-md-4">
    <h4 class="" style="color:#333;">
        <?php if ($block->getTitle()) : ?>
            <div class="block-title">
                <!-- THE FOLLOWING HREF IS WHERE I WOULD LIKE TO LINK TO THE PARENT CATEGORY OF THE LIST -->
                <a href="<?= $block->getCategoryUrl() ?>"><strong><?= $block->escapeHtml(__($block->getTitle())) ?></strong></a>
            </div>
        <?php endif ?>
    </h4>
</div>

Thanks for any suggestions on how to make this work. Let me know if anything is unclear, or if more information is needed.