Skip to content
Sitemason - Build on Us

Developer Tips: Custom Navigation

No matter how helpful Sitemason Library methods are, sometimes you gotta go custom. One common requirement is to break out from basic navigation menus to customize the display of a site's navigation. We of course recommend using printNavAsList() as it takes out SO much of the complication in writing your navigation. But when it just wont do, you can write your own. 

Here are three examples of writing custom navigation in PHP. A simple single level menu, then increasingly more complicated two and three level menus. Enjoy!

One Level Navigation

<ul>
<?php
    foreach ($content_xml->nav as $nav) { 
        echo '<li';
        if ($nav->currently_displayed || $nav->current_site) {
            echo ' class="active"';
        }
        echo '><a href="' . $nav->link . '">' . $nav->title . '</a></li>'; 
    }
?>
</ul>

Two Levels

<ul>
<?php
    foreach ($content_xml->nav as $nav) { 
        echo '<li';
        if ($nav->currently_displayed || $nav->current_site) {
            echo ' class="active"';
        }
        echo '><a href="' . $nav->link . '">' . $nav->title . '</a>';
        if ($nav->nav) {
            echo '<ul>';
            foreach ($nav->nav as $subnav) {
                echo '<li';
                if ($subnav->currently_displayed) {
                    echo ' class="active"';
                }
                echo '><a href="' . $subnav->link . '">' . $subnav->title . '</a></li>';
            }
            echo '</ul>';
        }
        echo '</li>'; 
    }
?>
</ul>

Three Levels

<div class="sm_nav">
    <ul class="l1">
        <?php
            $i=0; #set incrementing variable to zero
            foreach ($content_xml->nav as $nav) { #loop through main nav
                if (($i<5) && ($i>0)) { # grab the first 5 items in main nav excluding 
                    echo '<li';
                    if ($nav->currently_displayed) {
                        echo ' class="current"';
                    }
                    echo '><a href="' . $nav->link . '"';
                    if ($nav->currently_displayed) {
                        echo ' class="current"';
                    }                
                    echo'>' . $nav->title . '</a>';
                    if ($nav->nav) { # if section has subnavigation, repeat
                        echo '<ul class="l2">';
                        foreach ($nav->nav as $subnav) {
                            echo '<li';
                            if ($subnav->currently_displayed) {
                                echo ' class="current"';
                            }
                            echo '><a href="' . $subnav->link . '"';
                            if ($subnav->currently_displayed) {
                                echo ' class="current"';
                            }                
                            echo'>' . $subnav->title . '</a>';
                            if ($subnav->nav) { # if section has third level nav, repeat
                                echo '<ul class="l3">';
                                foreach ($subnav->nav as $third) {
                                    echo '<li';
                                    if ($third->currently_displayed) {
                                        echo ' class="current"';
                                    }
                                    echo '><a href="' . $third->link . '"';
                                    if ($third->currently_displayed) {
                                        echo ' class="current"';
                                    }                
                                    echo'>' . $third->title . '</a>';
                                    echo '</li>';
                                }
                                echo '</ul>';
                            }
                            echo '</li>';
                        }
                        echo '</ul>';
                    }
                    echo '</li>';
                }
                $i++;
            }
        ?>
    </ul>
</div>
Billy White headshot thumbnail

Billy is a web developer and Chief Product Officer (CPO) at Sitemason, and has been with the company since 2006.  Read more about Billy White.

Contact Billy:

Recent Forum Posts

Warning: file_get_contents(https://sitemason.zendesk.com/forums/279545/entries.json): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found in /mnt/files/s/sitemason.com/www/smTemplate/inc/support/zendesk-pull.php on line 11 Warning: usort() expects parameter 1 to be array, null given in /mnt/files/s/sitemason.com/www/smTemplate/inc/support/zendesk-pull.php on line 14 Warning: Invalid argument supplied for foreach() in /mnt/files/s/sitemason.com/www/smTemplate/inc/support/zendesk-pull.php on line 17