Drupal: How to Add Anchor to Menu
You need to include anchor link to Drupal menu (<a name="anchor"></a>
).
If you enter “node/15#anchor” to the “Path” field, Drupal will convert “#” to “%2523″ and you will get this on the output: “/node/15%2523anchor“. In this article I would tell you how to fix this problem.
Note:
Drupal shows ‘strict xhtml 1.1’ and anchors like this one <a name="anchor"></a>
don’t work.
We will replace this tag with ‘id’ tag in xhtml 1.1. This tag could be used for any tag.
For example, <p id="anchor"></p>
and you can refer to an anchor as usual: example.com#anchor
Options
- Show full URL
- Redefine output function to show links in menu
Show full URL
Anchors will work in menu links if you specify full URL (http://www.example.com/example#anchor instead of /example#anchor). But full path will make your site less extensible. So you can’t always use this solution.
Redefine output function to show links in menu
You need to redefine the function – add the following code to template.php of your theme:
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
);
}