sinanisler logo

Get WordPress header-menu named Menu and create as a shortcode

you can use this menu shortcode in pages, builders anywhere you want in WordPress.

 

shortcode [header_menu]

function header_menu_shortcode($atts) {
    // Define attributes and their defaults
    $atts = shortcode_atts(
        array (
            'menu' => 'header-menu',
            'container' => 'div',
        ),
        $atts, 
        'header_menu' 
    );
    
    // This adds the custom CSS class to the menu
    $atts['menu_class'] = 'custom_header_menu';
    
    $menu = wp_nav_menu(
        array (
            'menu' => $atts['menu'],
            'container' => $atts['container'],
            'menu_class' => $atts['menu_class'],
            'echo' => false
        )
    );

    // If the menu is not empty, add the styling
    if ($menu) {
        $menu .= '<style>
                    .custom_header_menu li a { color: red; font-size: 20px; }
                    .custom_header_menu .current-menu-item a { color: blue; font-weight: bold; }
                  </style>';
    }

    return $menu;
}
add_shortcode('header_menu', 'header_menu_shortcode');

 

Leave the first comment