【发布时间】:2011-12-02 13:13:51
【问题描述】:
我对 Drupal 6 和主题化还很陌生。我的主题需要主菜单为我合理参与的样式抽出相当广泛的 html/css。为了实现这一点,我拼凑了这个代码'function phptemplate_menu_item'(见底部),它位于我的template.php中,并根据菜单项是否有子项生成不同的html,并使用链接的内容生成一个css类.
我的问题是这段代码也被应用于我的其他菜单。我想让 menu_item 根据它是否在主菜单中生成不同的 html。我原以为最简单的方法是在函数 phptemplate_menu_item 中使用 if 语句,例如:
function phptemplate_menu_item (...){
if ($menu_name == 'primary-links')
{DO ABC}
else
{DO XYZ}
}
但是我相信我需要知道如何将菜单名称传递给 phptemplate_menu_item 函数。对此的任何帮助将不胜感激,因为我已经将头撞在墙上试图解决这个问题已经有一段时间了。
谢谢!
function phptemplate_menu_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
$class = ($menu ? 'no_wrap' : ($has_children ? 'collapsed' : 'li_wrap'));
if (!empty($extra_class)) {
$class .= ' '. $extra_class;
}
if ($in_active_trail) {
$class .= ' active-trail';
}
if (!empty($link)) {
/* The following section gives the list items unique classes based on their link text - note how spaces and sepcial chars are removed */
// remove all HTML tags and make everything lowercase
$css_id = strtolower(strip_tags($link));
// remove colons and anything past colons
if (strpos($css_id, ':')) $css_id = substr ($css_id, 0, strpos($css_id, ':'));
// Preserve alphanumerics, everything else goes away
$pattern = '/[^a-z]+/ ';
$css_id = preg_replace($pattern, '', $css_id);
$class .= ' '. $css_id;
}
// the following code returns the menu item formatted in a different fashion depending on the class of the item. The first one is for items with a class of none - ie the space li at end of menu
if (strstr($class, 'none')) {
return '<li class="'. $class . ' main"></span></span></li>';
}
if (strstr($class, 'li_wrap')) {
return '<li class="'. $class .' main"><span class="wrapped">'. $link . $menu ."<span class='indicator'></span></li>\n";
}
if (strstr($class, 'no_wrap')) {
return '<li class="'. $class . ' main">'. $link ."<span class='indicator'></span><span class='menu_box'><span class='menu_box_inner'>". $menu ."</span></span></li>\n";
}
}
【问题讨论】: