[英]Joomla: get active menu item
I'm trying to implement a custom menu in my joomla template. 我正在尝试在我的joomla模板中实现自定义菜单。 I added this line at the desired position in my
index.php
我在
index.php
中的所需位置添加了这一行
<?php include(dirname(__FILE__).DS.'/navi.php'); ?>
the navi.php
looks like this navi.php
看起来像这样
<?php
defined( '_JEXEC' ) or die( 'Restricted access');
$db = &JFactory::getDBO();
$query = "SELECT * FROM jos_menu WHERE menutype='mainmenu' AND published='1' AND sublevel='0' AND access='0' ORDER BY ordering ASC;";
$db->setQuery($query);
$navi = $db->loadAssocList();
$menu = &JSite::getMenu();
$active = $menu->getActive();
foreach ($navi as $item) {
if ($item['id'] == $active->id) {
$open = ' class="open"';
} else {
$open = '';
}
echo '<li'.$open.'><a href="'.$item['link'].'">'.$item['name'].'</a></li>';
}
?>
The problem is that the $active
variable only contains data when the frontpage is open. 问题在于,仅当打开首页时,
$active
变量才包含数据。 What's wrong with this code? 此代码有什么问题?
Thank you! 谢谢!
Found the bug: you need a different method to determine the url of the menu item: 发现了错误:您需要使用其他方法来确定菜单项的网址:
$url = JRoute::_($item['link'].'&Itemid='.$item['id']);
echo '<li'.$open.'><a href="'.$url.'">'.$item['name'].'</a></li>';
Thank you! 谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.