Category ID not matching with current category
Question
I'm displaying image of categories into my menu. It's working until the ID for the category is 10.
Pasted 10, the ID restart to 1, so my ID 11 is having the properties of the 1, like it's image (which I don't want do show).
Here's my topmenu.php
foreach ($children as $child) {
if ($childLevel === 0 && $child->getData('is_parent_active') === false) {
continue;
}
$categoryId = substr($child->getId(),-1); // Get CATEGORY ID
$category = $categoryFactory->create()->load($categoryId);
$child->setLevel($childLevel);
$child->setIsFirst($counter == 1);
$child->setIsLast($counter == $childrenCount);
$child->setPositionClass($itemPositionClassPrefix . $counter);
$outermostClassCode = '';
$outermostClass = $menuTree->getOutermostClass();
if ($childLevel == 0 && $outermostClass) {
$outermostClassCode = ' class="' . $outermostClass . '" ';
$currentClass = $child->getClass();
if (empty($currentClass)) {
$child->setClass($outermostClass);
} else {
$child->setClass($currentClass . ' ' . $outermostClass);
}
}
if (count($colBrakes) && $colBrakes[$counter]['colbrake']) {
$html .= '</ul></li><li class="column"><ul>';
}
$html .= '<li' . $this->_getRenderedMenuItemAttributes($child) . '>';
$html .= '<a href="' . $child->getUrl() . '" ' . $outermostClassCode . '>'
.'<span>'. $category->getId() .' - ' . $this->escapeHtml($child->getName())
. '</span><img src="' . $category->getImageUrl() . '"/></a>'. $this->_addSubMenu(
$child,
$childLevel,
$childrenWrapClass,
$limit
) . '</li>';
$itemPosition++;
$counter++;
}
How to change that to make my code work ?
Solution
I've changed :
$categoryId = substr($child->getId(),-1); // Get CATEGORY ID
$category = $categoryFactory->create()->load($categoryId);
To
$categoryIdentifiant = explode('-', $child->getId());
$categoryId = end($categoryIdentifiant);
$category = $categoryFactory->create()->load($categoryId);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange