Topmenu in Magento2 is generated from Block php file. That means when you want to override core Topmenu and make some changes to it, you need to create your own module.

Module structure

app
|
--code
     |
     --Customgateway
                   |
                   --Topmenu
                           |
                           --Block
                           |     |
                           |     --Rewrite
                           |             |
                           |             --Html
                           |                  |
                           |                  --Topmenu.php    
                           |
                           --etc
                           |   |
                           |   --di.xml
                           |   --module.xml  
                           |
                           --registration.php



app/code/Customgateway/Topmenu/Block/Rewrite/Html/Topmenu.php
(original file is under /vendor/magento/module-theme/Block/Html/Topmenu.php)

<?php
    namespace Customgateway\Topmenu\Block\Rewrite\Html;
 
    class Topmenu extends \Magento\Theme\Block\Html\Topmenu
    {
        protected function _getHtml(
            \Magento\Framework\Data\Tree\Node $menuTree,
            $childrenWrapClass,
            $limit,
            $colBrakes = []
        ) {

               //do your stuff here
        }
    }



app/code/Customgateway/Topmenu/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Theme\Block\Html\Topmenu" type="Customgateway\Topmenu\Block\Rewrite\Html\Topmenu" />
</config>



app/code/Customgateway/Topmenu/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Customgateway_Topmenu" setup_version="1.0.0">
    </module>
</config>



app/code/Customgateway/Topmenu/registration.php

<?php
  use \Magento\Framework\Component\ComponentRegistrar;
  ComponentRegistrar::register(ComponentRegistrar::MODULE, 'Customgateway_Topmenu', __DIR__);



!!!THERE IS OPENED BUG IN MAGENTO!!! described here: https://github.com/magento/magento2/issues/4564

To fix this, under your own theme in file
/htdocs/app/design/frontend/Customgateway/entertainer/Magento_Theme/layout/default.xml

change

  <block class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav" template="html/topmenu.phtml" ttl="3600" before="-"/>

to

  <block class="Magento\Theme\Block\Html\Topmenu" name="catalog.topnav" template="Magento_Theme::html/topmenu.phtml" ttl="3600" before="-"/>




after you upload files to server, do this
php bin/magento setup:upgrade
bin/magento setup:di:compile
sudo chmod 777 var -R
sudo chmod 777 pub -R


probably you would need to deploy static content again
php bin/magento setup:static-content:deploy en_GB --theme Magento/backend
php bin/magento setup:static-content:deploy en_GB --theme Customgateway/entertainer (your own theme)