I have a template from a customer’s Magento 1 theme that I need to move to M2. The theme isn’t the same but the template does what I need it to on M1 and I need this in Magento 2.
< ?php
$parentCategoryId = 20;
$cat = Mage::getModel('catalog/category')->load($parentCategoryId);
$subcats = $cat->getChildren();
foreach(explode(',',$subcats) as $subCatid){
$_category = Mage::getModel('catalog/category')->load($subCatid);
if($_category->getIsActive()) {
$image = Mage::getBaseUrl('media') . 'catalog/category/' . $_category->getThumbnail();
$imageCheck = $_category->getThumbnail();
$name = $_category->getName();
echo '- ';
echo '';
if ($imageCheck == ''){ $image = $this->getSkinUrl('images/noPic.jpg'); }
echo '';
echo '
' . $name . '
';
echo '';
echo ' ';
}
}
?>
This is PHTML from M1, which I did not write. In effect, it loads all subcategories of a parent category then displays them in a grid with the category title and thumbnail. If I have to create a small extension then call what I need in a PHTML, that’s fine but I’m still a little new to M2 so I’m not sure where to start.
I did find this answer and I’m using the top bit with the object manager: https://magento.stackexchange.com/a/201951/69191
I know that’s probably not the best practice but is it passable in a template file, or is there a better way?