Add custom links in opencart top menu - opencart 1.5.x

Posted by Flo on 21 Jun 2014

This small tutorial will show you how to manually add custom links to the default top menu in opencart. If you dont want to go trough all the hustle and would like to be able to completly customize your top menu have a look at my Supermenu module.

To add a link to the default top category menu in opencart you will have to edit the file catalog/view/theme/yourtheme/template/common/header.tpl and if you have the default theme or some theme that didnt modify the top menu structure, in header.tpl the code of the top menu will look something like this:

<?php if ($categories) { ?>
<div id="menu">
  <ul>
    <?php foreach ($categories as $category) { ?>
    <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 } ?>

You can add custom links before or after the opencart categories in the menu. In the above code you will see a custom link placed before the categories and one after, and using the same structure you can add more custom links by copy/pasting the line(s) and then modify the url and the name of the link.

<?php if ($categories) { ?>
<div id="menu">
  <ul>
   <li><a href="link-url">Link Name(before categories)</a></li>
    <?php foreach ($categories as $category) { ?>
    <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 } ?>
   <li><a href="link-url">Link Name(after categories)</a></li>
  </ul>
</div>
<?php } ?>

If you are using my news/blog module and you want to add a link to the blog headlines page to the top menu and you dont have supermenu , this is the way to do it.