menu Archives - wiki

WordPress get menu array

How to get the wordpress menu array

menu structure

BY SlUG

<?php 

$menu_name = 'sidebar-menu'; //menu slug
$locations = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );

echo "<pre>";
print_r($menuitems);
echo "</pre>";

?>

BY LOCATION

$menu_name = 'primary'; //Location 
$locations = get_nav_menu_locations();
$menu =  wp_get_nav_menu_object($locations[ $menu_name ]) ;
$menuitems = wp_get_nav_menu_items( $menu->term_id, array( 'order' => 'DESC' ) );

Output

Array
(
    [0] => WP_Post Object
        (
            [ID] => 22
            [post_author] => 1
            [post_date] => 2016-06-21 12:58:35
            [post_date_gmt] => 2016-06-21 12:58:35
            [post_content] =>  
            [post_title] => 
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => 22
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2016-06-22 05:18:52
            [post_modified_gmt] => 2016-06-22 05:18:52
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/wordpress/?p=22
            [menu_order] => 1
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 22
            [menu_item_parent] => 0
            [object_id] => 2
            [object] => page
            [type] => post_type
            [type_label] => Page
            [url] => http://localhost/wordpress/sample-page/
            [title] => Sample Page
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

    [1] => WP_Post Object
        (
            [ID] => 29
            [post_author] => 1
            [post_date] => 2016-06-22 05:18:52
            [post_date_gmt] => 2016-06-22 05:18:52
            [post_content] =>  
            [post_title] => 
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => 29
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2016-06-22 05:18:52
            [post_modified_gmt] => 2016-06-22 05:18:52
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/wordpress/?p=29
            [menu_order] => 2
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 29
            [menu_item_parent] => 0
            [object_id] => 24
            [object] => page
            [type] => post_type
            [type_label] => Page
            [url] => http://localhost/wordpress/page-abc/
            [title] => page abc
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

    [2] => WP_Post Object
        (
            [ID] => 28
            [post_author] => 1
            [post_date] => 2016-06-22 05:18:52
            [post_date_gmt] => 2016-06-22 05:18:52
            [post_content] =>  
            [post_title] => 
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => 28
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2016-06-22 05:18:52
            [post_modified_gmt] => 2016-06-22 05:18:52
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/wordpress/?p=28
            [menu_order] => 3
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 28
            [menu_item_parent] => 29
            [object_id] => 26
            [object] => page
            [type] => post_type
            [type_label] => Page
            [url] => http://localhost/wordpress/page-def/
            [title] => page def
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

    [3] => WP_Post Object
        (
            [ID] => 30
            [post_author] => 1
            [post_date] => 2016-06-22 05:18:53
            [post_date_gmt] => 2016-06-22 05:18:53
            [post_content] =>  
            [post_title] => 
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => 30
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2016-06-22 05:18:53
            [post_modified_gmt] => 2016-06-22 05:18:53
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://localhost/wordpress/?p=30
            [menu_order] => 4
            [post_type] => nav_menu_item
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
            [db_id] => 30
            [menu_item_parent] => 0
            [object_id] => 8
            [object] => page
            [type] => post_type
            [type_label] => Page
            [url] => http://localhost/wordpress/
            [title] => xcvxxv
            [target] => 
            [attr_title] => 
            [description] => 
            [classes] => Array
                (
                    [0] => 
                )

            [xfn] => 
        )

)
  • If the menu_item_parent equals 0 (false) that manse that menu is a root menu
  • menu_item_parent hold the parent menu id if it is not 0

 

ref: https://codex.wordpress.org/Function_Reference/wp_get_nav_menu_object

https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items/

By bm on June 22, 2016 | wordpress | A comment?
Tags: ,

WordPress custom menu

How to create WordPress custom menu

Step 1. register_nav_menu()

In functions.php add the following code

function register_my_menu() {
    register_nav_menu('my-new-menu',__( 'My New Menu' ));
}

add_action( 'init', 'register_my_menu' );

I after adding this code the new menu will started showing in admin (Appearance->menu)

wordpress custome menu admin

We can register multiple menus at same time

register_nav_menus(
            array(
                'mnu-1' => 'Menu One',
                'mnu-2' => 'Menu Two',
                 )
);

Step 2. wp_nav_menu()

Call ‘ wp_nav_menu()’ wherever you want to display the newly created menu

I am just adding in footer.php

<div id="footer-2">
    <?php wp_nav_menu( array( 'theme_location' => 'my-new-menu' ) ); ?>
</div>

 

new menu in footer

 

ref: https://codex.wordpress.org/Function_Reference/register_nav_menu

ref : https://codex.wordpress.org/Function_Reference/register_nav_menus

By bm on | wordpress | A comment?
Tags: ,