Advertising sustains the DA. Ads are hidden for members. Join today

Named anchors in menus

Last updated on
23 December 2016

Drupal 7 will no longer be supported after January 5, 2025. Learn more and find resources for Drupal 7 sites

In Drupal 7, a link to a named anchor or named region on the page can be created in the Menu system. Go to admin/structure/menu/manage/navigation/add. To link to an anchor on just one of your site's pages, set the path to the full url of the page and include the anchor reference, for example:

http://example.com/node/5#anchor

For a link to an anchor on the current page (that is, a link that works on every page the menu link is displayed on), you can use the Menu token module. Once that is installed together with the modules that it requires, you will be able to set the link path to:

[current-page:url]#anchor
or
node/[node:nid]#anchor

In earlier versions of Drupal, users who have tried using “named anchors,” also known as “jump links,” in the menu system can run into problems with them not working. A named anchor will look like this:
<a name="anchor"></a>

The expected behavior is that when a link that points to this named anchor is clicked, the user’s browser should jump to the anchor tag on the html page, making it appear at the top of the user’s browser window.

This desired behavior can be accomplished by adding the following function to your theme’s template.php file:

function phptemplate_menu_item_link($item, $link_item) {
  // Convert anchors in path to proper fragment
  $path = explode('#', $link_item['path'], 2);
  $fragment = !empty($path[1]) ? $path[1] : NULL;
  $path = $path[0];
  return l(
            $item['title'],
            $path,
            !empty($item['description']) ? array('title' => $item['description']) : array(),
            !empty($item['query']) ? $item['query'] : NULL,
            $fragment,
            FALSE,
            FALSE
          );
}

Help improve this page

Page status: No known problems

You can: