What are some best practices for highlighting the current menu item and its parent items in a PHP menu system?
One way to highlight the current menu item and its parent items in a PHP menu system is to use a combination of CSS classes and PHP logic to dynamically apply styling to the active menu items. You can achieve this by adding a specific class to the active menu item and its parent items based on the current page URL or menu item ID.
<?php
// Get the current page URL
$current_url = $_SERVER['REQUEST_URI'];
// Define an array of menu items with their corresponding URLs
$menu_items = array(
'Home' => '/',
'About' => '/about',
'Services' => '/services',
'Contact' => '/contact'
);
// Loop through the menu items and add a 'active' class to the current menu item and its parent items
foreach($menu_items as $menu_item => $url) {
$class = ($url == $current_url) ? 'active' : '';
echo '<li class="' . $class . '"><a href="' . $url . '">' . $menu_item . '</a></li>';
}
?>