How to use nice menu in drupal

本文介绍了一种使用PHP来动态生成并美化网站导航菜单的方法。通过解析菜单结构,为每个菜单项添加背景颜色和图片,并针对有子菜单的项构建了下拉列表。文中还展示了如何根据不同状态更改菜单项图标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

way 1:

function theme_nice_menu_build($menu) {
   $output = '';
   //  $backgroundColor = '#000000';
   $backgroundColor = 'transparent';

  foreach ($menu as $menu_item) {
    $mlid = $menu_item['link']['mlid'];
    // Check to see if it is a visible menu item.
    if ($menu_item['link']['hidden'] == 0) {
      // Build class name based on menu path
      // e.g. to give each menu item individual style.
      // Strip funny symbols.
      $clean_path = str_replace(array('http://', '<', '>', '&', '=', '?', ':'), '', $menu_item['link']['href']);
      // Convert slashes to dashes.
      $clean_path = str_replace('/', '-', $clean_path);
      $path_class = 'menu-path-'. $clean_path;
      
     //----begin edit 
      $imageTag = urldecode(theme('menu_item_link', $menu_item['link']));
      $menuArray = explode("|",$_GET['menu']);
      $sql_menulinks = db_query("SELECT link_title,link_stageon FROM {menu_links} where mlid='" . $mlid . "' and hidden=0 and menu_name!='navigation' and plid=0");
      $result_menulinks = db_fetch_array($sql_menulinks);
      $image_on = $result_menulinks['link_stageon'];
      if (in_array($mlid, $menuArray)) {
		if($result_menulinks && $result_menulinks['link_stageon']){
		        $imageTag = preg_replace("/src=\".*?\" onmouse/",'src="'.$image_on.'" onmouse',$imageTag);
			$imageTag = preg_replace("/src='.*?' onmouse/",'src="'.$image_on.'" onmouse',$imageTag);
			//$imageTag = preg_replace("/onmouseout=\"this.src='.*?'\"/","onmouseout=\"this.src='".$image_on."'\"",$imageTag);
		}		
      }
      //----end edit

      
     // If it has children build a nice little tree under it.
      if ((!empty($menu_item['link']['has_children'])) && (!empty($menu_item['below']))) {
        // Keep passing children into the function 'til we get them all.
        $children = theme('nice_menu_build', $menu_item['below']);
        // Set the class to parent only of children are displayed.
        $parent_class = $children ? 'menuparent ' : '';

	$output .= '<li id="menu-'. $mlid .'" class="'. $parent_class . $path_class .'" style="background-color:'.$backgroundColor.';z-index:1">'. $imageTag;
        // Build the child UL only if children are displayed for the user.
        // you can also do as children menu, at this script. i don't do it.  
        /*if ($children) {
          $output .= '<ul>';
          $output .= $children;
          $output .= "</ul>";
        }*/
        $output .= "</li>\n";
      }
      else {

        $output .= '<li id="menu-'. $mlid .'" class="'. $path_class .'" style="background-color:'.$backgroundColor.';">'. $imageTag .'</li>'."\n";
      }
    }
  }
  return $output;
}

way 2
the bold part can be replaced by the follow:
//echo $urlMenuArray['link_title'];
preg_match("/onmouseover=\"this\.src='([^']*)'\"/",$urlMenuArray['link_title'],$match);
//echo $match[1];
preg_match("/src='([^']*)'/",$urlMenuArray['link_title'],$match2);
//echo $match2[1];
$urlMenuArray['link_title'] = str_replace($match2[1],$match[1],$urlMenuArray['link_title']);

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值