Set active state on navigation dynamically

前端 未结 6 1213
遇见更好的自我
遇见更好的自我 2021-01-05 20:33

I seem to run into this problem frequently and can never find a solution. I have a Wordpress site with a top navigation. Since this is in my header.php, and used on all page

相关标签:
6条回答
  • 2021-01-05 21:07

    First, there is a css pseudo class prepared for styling 'active' links :

    a:active {css}
    

    For your situation, you would have to add this class to your styling :

    .active a, a:active {css}
    

    But your needs seems more on the PHP side than the CSS, perhaps someone else will help you with that part. There would be a javascript solution with jQuery, finding the actual location then inject a css selector to the proper element.

    Check this article and this other one about wordpress. It will help you.

    Stack Overflow references :

    • How do I target each menu different items on active state in Wordpress
    • How to add Active states and icons in wordpress wp_nav_menu()
    • Loosing Nav Active State in Wordpress Dynamic Menu
    • google search
    0 讨论(0)
  • 2021-01-05 21:07

    try something like this:

    <?php $pages = array('about' => 'About Us', 'blog' => 'blog') ?>
    
    <ul>
    <?php foreach($pages as $url => $page): ?>
        <?php $isActive = $_SERVER["REQUEST_URI"] == $url ?>
        <li<?php echo $isActive ? ' class="active"' : '' ?>>
            <a href="<?php echo $base_url . "/{$url}" ?>"><?php echo $page ?></a>
        </li>
    <?php endforeach ?>
    </ul>
    

    It may be worth looking into using wordpres functions such as get_page_link which would be nicer than using the Server super global as that's not nice. This would also fail if you have wordpress in a folder and not the document root, it's just a simple example to get you started :)

    0 讨论(0)
  • 2021-01-05 21:09

    you could try something along the lines of

    <li class="cemenu<?php echo ($_SERVER['PHP_SELF'] == '/about' ? ' active' : '');?>"><a href="<?php echo $base_url;?>/about">About</a></li>
    
    0 讨论(0)
  • 2021-01-05 21:10

    You could use preg_replace() to add class="active" like this:

    ob_start();
    echo '<ul>
      <li><a href="page1.php">Page 1</a></li>
      <li><a href="page2.php">Page 2</a></li>
      </ul>';
    $output = ob_get_clean();
    $pattern = '~<li><a href="'.$url.'">~';
    $replacement = '<li class="active"><a href="'.$url.'">';
    echo preg_replace($pattern, $replacement, $output);
    
    0 讨论(0)
  • 2021-01-05 21:18

    You can do this way:

    This will add the active class to the <a> which contains the page from the url.

    $(function(){
       var url = window.location.href;
       var page = url.substr(url.lastIndexOf('/')+1);
       $('.cemenu a[href*="'+page+'"]').addClass('active');
    });
    

    and if you want to add class to its parent li the replace the last line to this and css class should be like this:

    .active a{
          css 
          properties 
          for active li's a
    }
    
     // using closest
    $('.cemenu a[href*="'+page+'"]').closest('li').addClass('active'); 
    

    or

    // using parent
    $('.cemenu a[href*="'+page+'"]').parent('li').addClass('active');
    

    just tryout the fiddle here

    0 讨论(0)
  • 2021-01-05 21:24

    You can try like this

    <li class="<?php 
                    if($this_page=='Home'){
                         echo 'active';
                    }
              ?>">
          Home
     </li>
    <li class="<?php 
                    if($this_page=='Contact'){
                         echo 'active';
                    }
              ?>">
          Contact
     </li>
    

    And then in your home page

    $this_page='Home';
    

    And in your contact page

    $this_page='Contact';
    
    0 讨论(0)
提交回复
热议问题