Add a custom menu in your page.tpl.php
First off in our Drupal Template Snippets series is this HOWTO. You can add a custom menu in your page regions with a block, but sometimes you might want to make sure the menu is always there. And you do not want to worry about someone accidentally removing the menu block from your site. Well, here is a snippet to add a custom made menu to your page.tpl.php.
Let's assume your theme is named yourthemename and you named your menu "Menu Header". Drupal will create a system name for your menu based on the name you give it. In this case it will be menu-menu-header. Add the following snippet to your template.php:
function yourthemename_preprocess_page(&$vars) {
// Get the menu and add it to the $vars
$vars['my_custom_menu'] = menu_navigation_links('menu-menu-header', 0);
}
The menu_navigation_links will give you an array of links for a navigation menu. Now edit your page.tpl.php and add the following snippet where you want the menu to appear:
<?php if($my_custom_menu):?>
<?php print theme('links__menu_menu_header', array('links' => $my_custom_menu)); ?>
<?php endif;?>
Now this needs some explanation. First line is easy, we check if the variable is not empty. If there is a menu we want to theme it with the theme function for links. In Drupal 7 however, you have the ability to override the default implementation of theme('links', $vars). You can add a string to links which defines theme suggestions. You do that by adding "__" and the theme name you wish to use. So in our example, we use "links__menu_menu_header".
It will now look for "yourthemename_links__menu_menu_header()" as a theme function, enabling to customize the look and feel of the links for just this menu. But when this theme function is not present, it will automatically use the default implementation of theme('links'). The default implementation can be found on api.drupal.org. A good starting point is to copy the default implementation, to your custom theme implementation and adjust whatever you want there. Don't forget to clear the cache when adding the theme function!

Reactie toevoegen