How to add “active” class to wp_nav_menu() current menu item (simple way)

前端 未结 5 2075
慢半拍i
慢半拍i 2020-12-04 09:17

I am creating custom Wordpress theme using a starter theme _Underscores. I am also using Bootstrap as a front-end framework.

I would like to modify wp_nav_menu so th

5条回答
  •  执念已碎
    2020-12-04 09:42

    If you want the 'active' in the html:

    header with html and php:

             $menu_item ) {
                if ( ! $menu_item->menu_item_parent ) {
                    echo "
  • "; echo $menu_item->title; echo "
  • "; } } ?>

    functions.php:

    function vince_check_active_menu( $menu_item ) {
        $actual_link = ( isset( $_SERVER['HTTPS'] ) ? "https" : "http" ) . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        if ( $actual_link == $menu_item->url ) {
            return 'active';
        }
        return '';
    }
    

提交回复
热议问题