wp_get_nav_menu_items() Current Menu Item - WP Robbert

PHOTO EMBED

Thu Apr 08 2021 12:12:31 GMT+0000 (UTC)

Saved by @francis_cubi #php

// Check if menu exists
if ( $menu_items = wp_get_nav_menu_items( 'menu' ) ) {
   
   // Loop over menu items
   foreach ( $menu_items as $menu_item ) {

      // Compare menu item url with server request uri path
      $current = ( $_SERVER['REQUEST_URI'] == parse_url( $menu_item->url, PHP_URL_PATH ) ) ? 'current' : '';

      // Print menu item
      echo '<li class="' . $current . '"><a href="' . $menu_item->url . '">' . $menu_item->title . '</a></li>';
   }
}
content_copyCOPY

https://www.robbertvermeulen.com/wp_get_nav_menu_items-current-menu-item/