我总是想知道为什么在核心中没有这个功能,但afaik没有。
所以看起来我们需要推出我们自己的,走一个完整的菜单树,直到我们找到我们需要的子树:
/**
* Extract a specific subtree from a menu tree based on a menu link id (mlid)
*
* @param array $tree
* A menu tree data structure as returned by menu_tree_all_data() or menu_tree_page_data()
* @param int $mlid
* The menu link id of the menu entry for which to return the subtree
* @return array
* The found subtree, or NULL if no entry matched the mlid
*/
function yourModule_menu_get_subtree($tree, $mlid) {
// Check all top level entries
foreach ($tree as $key => $element) {
// Is this the entry we are looking for?
if ($mlid == $element['link']['mlid']) {
// Yes, return while keeping the key
return array($key => $element);
}
else {
// No, recurse to children, if any
if ($element['below']) {
$submatch = yourModule_menu_get_subtree($element['below'], $mlid);
// Found wanted entry within the children?
if ($submatch) {
// Yes, return it and stop looking any further
return $submatch;
}
}
}
}
// No match at all
return NULL;
}
要使用它,你首先需要获得树的整个菜单,使用menu_tree_page_data()
或menu_tree_all_data()
,这取决于你需要什么(检查差异的API定义)。然后根据mlid提取你想要的子树。此子树然后可以通过menu_tree_output()
呈现为HTML:
$mlid = 123; // TODO: Replace with logic to determine wanted mlid
$tree = menu_tree_page_data('navigation'); // TODO: Replace 'navigation' with name of menu you're interested in
// Extract subtree
$subtree = yourModule_menu_get_subtree($tree, $mlid);
// Render as HTML menu list
$submenu = menu_tree_output($subtree);
免责声明:我不知道这是做一个好/有道 - 这就是我想出了一个解决方案在经历与OP相同的过程之后,即通过阅读整个菜单模块的功能,总是想知道我是否错过了明显的某处...
您不应该直接调用主题函数。 <?php print theme('menu_tree',69); ?> 本来是得到意想不到的结果的正确方法:) – Rimian 2009-12-06 04:21:54