• Hello,

    long time ago I made this PHP code to show menu items with their thumnails.

    <?php
        function get_the_post_thumbnail_src($img)
    {
      return (preg_match('~\bsrc="([^"]++)"~', $img, $matches)) ? $matches[1] : '';
    }
    // Get the nav menu based on $menu_name (same as 'theme_location' or 'menu' arg to wp_nav_menu)
    // This code based on wp_nav_menu's code to get Menu ID from menu slug
    $menu_name = 'menu-homepage'; // change this if necessary
    if ( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ) {
        $menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
        $menu_items = wp_get_nav_menu_items($menu->term_id);
        $menu_list = '<ul id="menu-' . $menu_name . '">';
        foreach ( (array) $menu_items as $key => $menu_item ) {
            $title = $menu_item->title;
            $url = $menu_item->url;
            $mid = $menu_item->object_id;
            if(has_post_thumbnail($mid)) {
                $thumbnail = get_the_post_thumbnail( $mid, 'thumb' );
                $image_url = get_the_post_thumbnail_src(get_the_post_thumbnail( $mid, 'thumb' ));
            }
            $image_url2=substr($image_url, -17);
            if(!$i)
            $i=1;
            else
            $i++;
            if($i==1 or $i==4 or $i==7)
            $styl="style='clear:both'";
            else
            $style="";
            if($image_url2=="pozadiPrazdne.png")
            $menu_list .= '<div class="liWrapperMaly".'.$styl.'"><a href="' . $url . '"><li style="background-image:url(\''.$image_url.'\');"><a href="' . $url . '"><h2>' . $title . '</h2></li></a></div>';
            else
            $menu_list .= '<div class="liWrapper".'.$styl.'"><a href="' . $url . '"><li style="background-image:url(\''.$image_url.'\');"><h2>' . $title . '</h2></li></a></div>';
        }
        $menu_list .= '</ul>';
     } else {
        $menu_list = '<ul><li>Menu "' . $menu_name . '" not defined.</li></ul>';
     }
     // $menu_list now ready to output
     echo $menu_list;
     ?>

    Now I want to display another menu by same method, so I simply change this line

    $menu_name = 'menu-homepage';

    to this line

    $menu_name = 'new-menu';

    In functions.php I registered new-menu

    register_nav_menu('new-menu', 'New Menu');

    New Menu is created in WordPress administration, but if I execute this PHP code it writes me this message “Menu new-menu not defined”. But I defined it by

    register_nav_menu('new-menu', 'New Menu');

    didn’t I? So where can be problem?

    Thanks a lot for every advice.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘How to register menu?’ is closed to new replies.