Drupal:Using of Primary/Secondary Links for Building of Split-Level Menu

You need:
  • Create split-level menu.
  • The upper string is main items (first level), and sub-menu appears to the left of the selected item.
  • Parent item from main menu items is active when navigation through submenus.

 

Solution:

We need to form one menu creating its structure through selection of parent items, and then make the same menu primary and secondary in menu settings and add location of these menus in the form of ‘links’in  page.tpl.php.

Please follow step-by-step instructions to do this::

  1. GO to Administer → Site Building → Menus
  2. Create menu (you can create the required items just in primary links – menu is already created by default). To make menu split-level, select parents for menu items. Do nor create 2 different menus.
  3. Open Settings tab and select:
    Menu with primary links: your menu (Primary links)
    Menu with secondary links: your menu (Primary links)
  4. Insert the following code to page.tpl.php:
    For primary_links

<?php if (isset($primary_links)) { ?><?php print theme(‘links’, $primary_links) ?><?php } ?>

          For secondary_links:

<?php if (isset($secondary_links)) { ?><?php print theme(‘links’, $secondary_links) ?><?php } ?>

This method can be used for two-level hierarchy i.e. there are upper items and left items. No other submenus – secondary menu won’t show them.

We will use TemplatePHP  snippet from this page to create parent link with class="active". Insert the following code (without closing ?>) to template.php.

<?php
function phptemplate_links($links, $attributes = array()) {
  if (!count($links)) {
    return ”;
  }
  $level_tmp = explode(‘-’, key($links));
  $level = $level_tmp[0];
  $output = "<ul class=\"links-$level ".$attributes['class']. "\">\n";
  foreach ($links as $index => $link) {
    $output .= ‘<li’;
    if (stristr($index, ‘active’)) {
      $output .= ‘ class="active"’;
    }// frontpage AND current-link in menu is <front>
    elseif((drupal_is_front_page()) && ($link['href']==’<front>’)){
      $link['attributes']['class'] = ‘active’;//add class active to <li
      $output .= ‘ class="active"’;//add class active to <a
    }
    $output .= ">". l($link['title'], $link['href'], $link['attributes'], $link['query'], $link['fragment']) ."</li>\n";
  }
  $output .= ‘</ul>’;
  return $output;
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值