• Hi,

    I’ve a problem to show a Custom Menu in sidebar.
    Now, I’ve created a Menu from WordPress back-end. this menu must be showed in sidebar of my template, with menu name (i.e. CUSTOM MENU) and the structure with parents and child.
    My target is this.

    At the moment, I’ve this part of code:

    $meta_box_menu = array(
        'id' => 'custom-meta-menu',
        'title' => 'Menu Sidebar',
        'page' => 'page',
        'context' => 'side',
        'priority' => 'high',
        'fields' => array(
            array(
                'id' => 'custom-meta-menu-name',
                'type' => 'select',
                'std' => 'none'
            ),
        ),
    );
    
    /*
    * This function will register the meta box with WordPress
    */
    function custom_add_box() {
        global $meta_box_menu;
        add_meta_box($meta_box_menu['id'], $meta_box_menu['title'], 'custom_meta_menu_html', $meta_box_menu['page'], $meta_box_menu['context'], $meta_box_menu['priority']);
    }
    add_action('admin_init', 'custom_add_box');
    
    /*
    * This function will produce the html needed to display our meta box in the admin area
    */
    function custom_meta_menu_html() {
      global $meta_box_menu, $post;
    
      $output = '<p style="padding:10px 0 0 0;">'.__('Scegli il menu da mostrare nella Sidebar di questa pagina.', 'custom').'</p>';
      $output .= '<input type="hidden" name="sf_meta_box_nonce" value="'. wp_create_nonce(basename(__FILE__)). '" />';
    
      $output .= '<table class="form-table">';
    
      foreach ($meta_box_menu['fields'] as $field) {
        $meta = get_post_meta($post->ID, $field['id'], true);
    
        /*
        *   Get out all our menus using the function from functions.php
        */
        $menus = custom_get_all_menus();
    
        /*
        *   Grab out saved data for edit mode
        */
        $meta = get_post_meta($post->ID, $field['id'], true);
    
        $output .= '<select name="'.$field['id'].'" class="widefat">';
        $output .= '<option value="none">- none -</option>';
        if(is_array($menus)):
          foreach($menus as $k => $v):
            if($meta==$v->slug):
              $output .= '<option selected="selected" value="' . $v->slug .'">' . $v->name . '</option>';
            else:
              $output .= '<option value="' . $v->slug .'">' . $v->name . '</option>';
            endif;
          endforeach;
        endif;
        $output .= '</select>';
    
      }
    
      $output .= '</table>';
    
      echo $output;
    }
    
    /*
    * This function will save our preferences into the database
    */
    function custom_save_data($post_id) {
    
      global $meta_box, $meta_box_menu;
    
        foreach ($meta_box_menu['fields'] as $field) {
          $old = get_post_meta($post_id, $field['id'], true);
          $new = $_POST[$field['id']];
    
          if ($new && $new != $old) {
            update_post_meta($post_id, $field['id'], stripslashes(htmlspecialchars($new)));
          } elseif ('' == $new && $old) {
            delete_post_meta($post_id, $field['id'], $old);
          }
        }
    }
    add_action('save_post', 'custom_save_data');
    
    function custom_get_all_menus() {
      $menu_obj = get_terms( 'nav_menu' );
      return $menu_obj;
    }
    
    /*
    * Html Custom Sidebar Menu
    */
    function custom_generate_menu_from_items($items) {
      if(count($items)>0):
        $menu_list = '<aside class="widget sidebar_menu"><h5 class="widget-title">' .'CUSTOM MENU'. '</h5><nav><ul class="sidebar-menu-items">';
          foreach ( (array) $items as $key => $menu_item ) {
            $title = $menu_item->title;
            $url = $menu_item->url;
            $menu_list .= '<li><a href="' . $url . '">' . $title . '</a></li>';
          }
        $menu_list .= '</ul></nav></aside>';
        return $menu_list;
    	$menu_item->title = get_post_meta( $menu_item->ID, '_menu_item_title', true );
    	    return $menu_item;
      endif;
      add_filter( 'wp_get_nav_menu_items', 'custom_generate_menu_from_items', null, 3 );
    }

    With this code, the menu is showed in output page but the pages are placed in the same level, without parent child relationship.
    How can I keep this relationship?

    Thank you your support.

  • The topic ‘Custom Menu in Sidebar’ is closed to new replies.