Customize WordPress menus in own way

Posted on March 10, 2013

Creating WordPress Navigation Menus is much easy from Dashboard -> Appearance -> Menu. Most of the time, it requires to customize these menus according to theme design and it can be done with the WordPress  function wp_nav_menu() . Although, we can populate these menu with our own way and style menu accordingly. Here is the code:

<?php
$menu = wp_get_nav_menu_object( 'Main Menu' ); // This is the menu name.
$menu_items = wp_get_nav_menu_items($menu-&gt;term_id);
foreach ( $menu_items as $menu_item ) {
$nav_menus[] = $menu_item-&gt;object_id;
}
$menus_str = implode(',', $nav_menus);
$pages = get_pages(array( 'sort_column' =&gt; 'menu_order', 'sort_order' =&gt; 'asc', 'include' =&gt; $nav_menus ));
echo "<ul>";
foreach ( $pages as $p ) {
?>
<li><a href="<?php echo get_page_link( $p-&gt;ID ); ?>"><?php echo $p->post_title; ?></a></li>
<?php
}
echo "</ul>";
?>