简体   繁体   中英

Add a class to a menu item in WordPress

I am creating a theme in WordPress. I want to add a class to a <li> element if the user is on that page.

I have managed to create a dynamic navigation, using following code in my header.php :

<div class="nav">
    <?php wp_nav_menu( array( 'theme_location' => 'nav-menu' ) ); ?>
</div>

Which in HTML, translates to:

<div class="nav">
   <ul>
      <li> Home </li>
      <li> Products </li>
      <li> About </li>
   </ul>
</div>

I am hoping to dynamically alter this. If the user is on www.website.com/about, the navigation will change to:

   <div class="nav">
       <ul>
          <li> Home </li>
          <li> Products </li>
          <li class="underline"> About </li>
       </ul>
    </div>

According to the docs, the current page item should already have a class .current-menu-item . you can use that to style the item with underline. Do you not see this class?

http://codex.wordpress.org/Function_Reference/wp_nav_menu#Current-Page_Menu_Items

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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