wp_get_nav_menu_items() Current Menu Item - WP Robbert

PHOTO EMBED

Thu Apr 08 2021 12:11:32 GMT+0000 (Coordinated Universal Time)

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 object with current page menu object
      $current = ( $menu_item->object_id == get_queried_object_id() ) ? '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/