星期三 , 22 1 月 2025

用wp_nav_menu输出自定义的菜单样式和添加active

$menu = wp_get_nav_menu_items('menu-1');
$current_url = is_home() || is_front_page() ? home_url('/') : get_permalink();

foreach ($menu as $item) {
    $item_url = $item->url;
    $active_class = ($current_url === $item_url) ? 'active' : '';

    ?>
    <div class="slide <?php echo $active_class; ?>">
        <a href="<?php echo $item_url; ?>"><?php echo $item->title; ?></a>
    </div>
    <?php
}