简体   繁体   English

将活动类添加到php生成的菜单中的所选链接

[英]Add active class to the selected link in php generated menu

I'm want to add class="active" in list item to highlight the selected menu but I couldn't figure it out how to do this since I'm not really familiar with php. 我想在列表项中添加class =“ active”以突出显示所选菜单,但由于我对php不太熟悉,所以我不知道该怎么做。

this is what I have in tpl file 这就是我在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>

and in php file 并在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('&nbsp;&nbsp;&nbsp;&nbsp;',$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);
    }
}

Thank you in advance! 先感谢您!

In your tpl, add $class (I put it with $target , but it can be anywhere inside the <a> ) 在您的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>

The in your php file add 在您的php文件中添加

$current_url = 'http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

AND

$template->set('class',(($current_url == $link['url']) ? ' class="active"' : ''));

this check to see if the current url is the same as the $link['url'] , and adds `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('&nbsp;&nbsp;&nbsp;&nbsp;',$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);
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM