我想在列表项中添加class =“active”以突出显示所选菜单但我无法弄清楚如何执行此操作,因为我对php并不熟悉。
这就是我在tpl文件中的内容
<ul class="menu">
<li>
<a<?php echo $target; ?> href="<?php echo $link; ?>"<?php echo $nofollow; ?>><?php echo $this->escape_html($link_title); ?></a>
<?php echo $sub_menu; ?>
</li>
并在php文件中
$links = $this->db->GetAll($query);
foreach($links as $link) {
$template = $this->PMDR->getNew('Template');
$template->set('link',$link['url']);
$template->set('link_title',$link['title']);
$template->set('nofollow',($link['nofollow'] ? ' rel="'.$link['nofollow'].'"' : ''));
$template->set('target',($link['target'] ? ' target="'.$link['target'].'"' : ''));
$template->set('indent',str_repeat(' ',$level));
ob_start();
$this->getMenuLoop($link['id'], $level+1);
$sub_menu = ob_get_clean();
if(!empty($sub_menu)) {
$menu_template = $this->PMDR->getNew('Template',$this->template);
$menu_template->set('items', $sub_menu);
$sub_menu = $menu_template->render();
}
$template->set('sub_menu', $sub_menu);
echo $template->render($this->item_template);
}
}
提前谢谢!
答案 0 :(得分:0)
在你的tpl中,添加$class
(我把它放在$target
,但它可以在<a>
内的任何地方)
<ul class="menu">
<li>
<a<?php echo $target; echo $class; ?> href="<?php echo $link; ?>"<?php echo $nofollow; ?>><?php echo $this->escape_html($link_title); ?></a>
<?php echo $sub_menu; ?>
</li>
在你的php文件中添加
$current_url = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
AND
$template->set('class',(($current_url == $link['url']) ? ' class="active"' : ''));
检查当前网址是否与$link['url']
相同,并添加“class =”active“
//gets the current page
$current_url = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$links = $this->db->GetAll($query);
foreach($links as $link) {
$template = $this->PMDR->getNew('Template');
$template->set('link',$link['url']);
$template->set('link_title',$link['title']);
$template->set('nofollow',($link['nofollow'] ? ' rel="'.$link['nofollow'].'"' : ''));
$template->set('target',($link['target'] ? ' target="'.$link['target'].'"' : ''));
$template->set('class',(($current_url == $link['url']) ? ' class="active"' : ''));
$template->set('indent',str_repeat(' ',$level));
ob_start();
$this->getMenuLoop($link['id'], $level+1);
$sub_menu = ob_get_clean();
if(!empty($sub_menu)) {
$menu_template = $this->PMDR->getNew('Template',$this->template);
$menu_template->set('items', $sub_menu);
$sub_menu = $menu_template->render();
}
$template->set('sub_menu', $sub_menu);
echo $template->render($this->item_template);
}
}