我總是想知道爲什麼在覈心中沒有這個功能,但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