在制作多语言的站点的时候
发现很多主题都不能完整的控制到i18n的菜单输出
于是自己就找了一些资料来分析
并最终写出下列的代码
这段代码实现了 只输出当前语言和所有语言的菜单 树
应用在template.php里面
function i18n_menu_tree_output($tree) { $output = ''; $items = array(); global $language; // 用于当前的语言判断 $current_language = $language->language; foreach ($tree as $data) { //只显示当前语言和全局语言 if (!$data['link']['hidden'] && ($data['link']['options']['langcode'] == $current_language || empty($data['link']['options']['langcode']))) { $items[] = $data; } } $num_items = count($items); foreach ($items as $i => $data) { $extra_class = NULL; if ($i == 0) { $extra_class = 'first'; } if ($i == $num_items - 1) { $extra_class = 'last'; } $link = theme('menu_item_link', $data['link']); if ($data['below']) { $output .= theme('menu_item', $link, $data['link']['has_children'], i18n_menu_tree_output($data['below']), $data['link']['in_active_trail'], $extra_class); } else { $output .= theme('menu_item', $link, $data['link']['has_children'], '', $data['link']['in_active_trail'], $extra_class); } } return $output ? theme('menu_tree', $output) : ''; } //输出子菜单 function i18n_secondary_links($secondary_links_data) { $items = array(); global $language; // 用于当前的语言判断 $current_language = $language->language; foreach ($secondary_links_data as $link){ if($link['langcode'] == $current_language || empty($link['langcode'])) $items[]=$link; } $outprint=theme('links', $items, array('class' => 'links secondary-links')); return $outprint; } function phptemplate_preprocess_page(&$vars) { //输出主菜单 $menu_name = 'primary-links'; $tree = menu_tree_page_data($menu_name); $vars['primary_links_tree'] = i18n_menu_tree_output($tree); }
在page.tpl.php中输出
<? //在这里重新输出菜单树 php print $primary_links_tree; ?> <?php //在这里重新输出子菜单 print i18n_secondary_links($secondary_links); ?>
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛