0

リスト項目に 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('&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);
    }
}

前もって感謝します!

4

1 に答える 1

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'];

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

このチェックは、現在のURLがと同じであるかどうかを確認し、$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);
}

}

于 2012-09-13T22:05:07.320 に答える