phprockers-logo

How to get the menu items in wordpress?

Using wp_get_nav_menu_items() function, we can get all the items(post,page,category) in the menu.

<?php
    $menu_items = wp_get_nav_menu_items('64');
    $menu_list = '<div class="menu_list">';
    foreach ( (array) $menu_items as $key => $menu_item ) {
        $title = $menu_item->title;
        $url = $menu_item->url;
        $menu_list .= '<div><a href="' . $url . '">' . $title . '</a></div>';
    }
    $menu_list .= '</div>';
    echo $menu_list;
?>

0 comments:

Post a Comment