Opencart · 1 min read · Updated

Show the first four categories in the menu in OpenCart

first_four_category_menu ecommerce designs

Today we provide support to Opencart forum that someone appreciates my work as we have helped him with his e-commerce designs to show the first four menu items in Opencart.

We simply changed the code and posted it on the forum and his reply was:

You, my friend, are a legend! That is exactly what I was looking for. Thank you so much! its really appreciated. I just checked out your website, some useful information on there, bookmarked 

What was the problem and how I solved it?

Solution:

<?php if ($categories) { ?>
<div id="menu">
<ul>
<?php foreach ($categories as $key=>$category) { if($key<4){ ?>
<li><a href="<?php echo $category['href']; ?>"><?php echo $category['name']; ?></a>
<?php if ($category['children']) { ?>
<div>
<?php for ($i = 0; $i < count($category['children']);) { ?>
<ul>
<?php $j = $i + ceil(count($category['children']) / $category['column']); ?>
<?php for (; $i < $j; $i++) { ?>
<?php if (isset($category['children'][$i])) { ?>
<li><a href="<?php echo $category['children'][$i]['href']; ?>"><?php echo $category['children'][$i]['name']; ?></a></li>
<?php } ?>
<?php } ?>
</ul>
<?php } ?>
</div>
<?php } ?>
</li>
<?php } } ?>
</ul>
</div>
<?php } ?>

Changes done are bolded.

Rupak Nepali
Written byRupak Nepali

Rupak Nepali is a web developer, manage Webocreation website and the author of six books:
Getting Started with OpenCart Module Development (Packt, 2013),
OpenCart Theme and Module Development (Packt, 2015),
OpenCart 4: Dev Guide for Themes & Extensions (2024),
OpenCart 4: User Manual (2025),
Cloudflare for the Enterprise Website (2026) and
The Technical Web Guide for Marketers (2026).
Rupak has built OpenCart stores and extensions since 2010, has published more than 600 tutorials on Webocreation, and shares free OpenCart extensions on GitHub.
Find Rupak on
GitHub | Amazon | LinkedIn | X | YouTube.

Leave a Reply

Your email address will not be published. Required fields are marked *