Magento 2 – Show Content of PHTML File on Static Block

magento2navigationphtmlstatic-block

I have the code (details below) that basically generates links and images for a categories sub-categories. It is working in Magento 1.7 – we are moving to Magento 2 and I would like to get it working in Magento 2.0. Can anyone please suggest an update to get it working?

PHTML FILE LOCATION

The phtml file is located in (Magento 1.9) /template/catalog/navigation/categoryblocks.phtml

PHTML FILE CONTENTS

<?php 
        //If there are sub categories
        $categories = $this->getCurrentChildCategories();
        $categoriescount = $this->getCurrentChildCategories()->count();
        if ($categoriescount > 0):  ?>
        <div class="container_12">  
        <?php 
            //Loop through categories
            foreach ($categories as $category): ?>
            <div class="grid_3 alpha special-spacing" style="margin-bottom:10px;">
                <div>
                    <!--<p class="cat-link"><?php echo $category->getName()?></p>-->
                    <a href="<?php echo $this->getCategoryUrl($category) ?>" class="cat-link"><?php echo $category->getName()?></a>
                </div>
                <?php //get category products
                $categoryProducts = $category->getProductCollection();
                   if(count($categoryProducts) <> 1):?>
                <a href="<?php echo $this->getCategoryUrl($category)?>" class="cat-image">
                <?php else:?>
                <?php $product = $categoryProducts->getFirstItem();?>
                <a href="<?php echo $product->getProductUrl()?>" class="cat-image">
                <?php endif;?>
                <?php 
                // If there is a thumbnail set for the category - Display it
                if($imgUrl = Mage::getModel('catalog/category')->load($category->getId())->getImage()):?>
                <img src="<?php echo $this->getBaseUrl()."media/catalog/category/".$imgUrl ?>" alt="<?php echo $this->htmlEscape($category->getName()) ?>" />
                <?php endif; ?>
                </a>
            </div>

            <?php endforeach; ?>
        <div class="clear"></div>
        </div>
        <?php else:?>
        <p>No Sub Categories</p>

CURRENT CALL IN STATIC BLOCK

{{block type="catalog/navigation" template="catalog/navigation/categoryblocks.phtml"}}

Best Answer

you can keep below code,

Keep inside Static block below code,

{{block class="Magento\Catalog\Block\Navigation" templates="catalog/navigation/categoryblocks.phtml"}}

Below file keep inside your app/design/frontend/Vendorname/themename/Magento_Catalog/templates/catalog/navigation/categoryblocks.phtml

    <?php 
            //If there are sub categories
            $categories = $block->getCurrentChildCategories();
            $categoriescount = $block->getCurrentChildCategories()->count();
            if ($categoriescount > 0):  ?>
            <div class="container_12">  
            <?php 
                //Loop through categories
                foreach ($categories as $category): ?>
                <div class="grid_3 alpha special-spacing" style="margin-bottom:10px;">
                    <div>
                        <!--<p class="cat-link"><?php echo $category->getName()?></p>-->
                        <a href="<?php echo $block->getCategoryUrl($category) ?>" class="cat-link"><?php echo $category->getName()?></a>
                    </div>
                    <?php //get category products
                    $categoryProducts = $category->getProductCollection();
                       if(count($categoryProducts) <> 1):?>
                    <a href="<?php echo $block->getCategoryUrl($category)?>" class="cat-image">
                    <?php else:?>
                    <?php $product = $categoryProducts->getFirstItem();?>
                    <a href="<?php echo $product->getProductUrl()?>" class="cat-image">
                    <?php endif;?>
                    </a>
                </div>

                <?php endforeach; ?>
            <div class="clear"></div>
            </div>
            <?php else:?>
    <h3>product not 

found...</h3>
<?php endif;?>