سؤال

In order to apply the active class to the current item in the navigation menu. I need to check if its link is equal to the URL

I am trying like this:

<ul class="nav">
    <li class="<?php if(uri_string(current_url()) == base_url() || uri_string(current_url()) == '') echo 'active'; ?>">
         <a href="<?php echo base_url() ?>">Home</a>
    </li>
    <li class="<?php if(uri_string(current_url()) == base_url('news/create/')) echo 'active'; ?>">
        <a href="<?php echo base_url('news/create/') ?>">+ New</a>
    </li>
</ul>

Wich seems to work fine with the home

But in news/create won't ever be equal....

It compares news/create (uri_string(current_url()) with /news/create (base_url('news/create'))

So.. what's the way to go with this issue?

هل كانت مفيدة؟

المحلول

As I said, a clean and simple solution:

if($this->uri->uri_string() == 'news/create'){ ...

نصائح أخرى

Try this..

<ul class="nav">
    <li class="<?php if(! $this->uri->segment(1) || $this->uri->segment(1) == 'welcome') echo 'active'; ?>">
         <a href="<?php echo base_url() ?>">Home</a>
    </li>
    <li class="<?php if($this->uri->segment(1) == 'news' && $this->uri->segment(2) == 'create' ) echo 'active'; ?>">
        <a href="<?php echo base_url('news/create/') ?>">+ New</a>
    </li>
</ul>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top