Node matches primary menu link

Last modified: August 26, 2009 - 23:16

Here's a code snippet to find if the current node matches a primary menu link. This code assumes the primary menu links are of the form '/somepage'.

<?php if (count($primary_links)) : ?>
    <ul class="primary">
        <?php foreach ($primary_links as $link): ?>
            <?php preg_match("/<a\s*.*?href\s*=\s*['\"]([^\"'>]*).*?>(.*?)<\/a>/i", $link, $matches); ?>
            <?php if (('/'.drupal_get_path_alias('node/'.$node->nid))==$matches[1]): /* if $node exists */ ?>
                <li class="selected"><?php print $link?></li>
            <?php elseif ('/'.arg(0)==$matches[1]): /* else try to use arg */ ?>
                <li class="selected"><?php print $link?></li>
            <?php elseif ((drupal_get_path_alias('node/'.$node->nid)=='node/') && (arg(0)=='node') && ($matches[1]=='/')): /* else if home */ ?>
                <li class="selected"><?php print $link?></li>
            <?php else: ?>
                <li><?php print $link?></li>
            <?php endif; ?>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

This code might not do what you expect for psuedo nodes that are part of a module as it currently only uses arg(0).

 
 

Drupal is a registered trademark of Dries Buytaert.