0

I have read the following link about a tree menu which gives a good solution to do that, but the links are not nested

https://stackoverflow.com/a/4495016/3754884

I want edit this code for change to the nested link url :

 function get_menu_html( $root_id = 0 )
 {
$this->html  = array();
$this->items = $this->get_menu_items();

foreach ( $this->items as $item )
    $children[$item['parent_id']][] = $item;

// loop will be false if the root has no children (i.e., an empty menu!)
$loop = !empty( $children[$root_id] );

// initializing $parent as the root
$parent = $root_id;
$parent_stack = array();

// HTML wrapper for the menu (open)
$this->html[] = '<ul>';

while ( $loop && ( ( $option = each( $children[$parent] ) ) || ( $parent > $root_id ) ) )
{
    if ( $option === false )
    {
        $parent = array_pop( $parent_stack );

        // HTML for menu item containing childrens (close)
        $this->html[] = str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 ) . '</ul>';
        $this->html[] = str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 - 1 ) . '</li>';
    }
    elseif ( !empty( $children[$option['value']['id']] ) )
    {
        $tab = str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 - 1 );

        // HTML for menu item containing childrens (open)
        $this->html[] = sprintf(
            '%1$s<li><a href="%2$s">%3$s</a>',
            $tab,   // %1$s = tabulation
            $option['value']['link'],   // %2$s = link (URL)
            $option['value']['title']   // %3$s = title
        ); 
        $this->html[] = $tab . "\t" . '<ul class="submenu">';

        array_push( $parent_stack, $option['value']['parent_id'] );
        $parent = $option['value']['id'];
    }
    else
        // HTML for menu item with no children (aka "leaf") 
        $this->html[] = sprintf(
            '%1$s<li><a href="%2$s">%3$s</a></li>',
            str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 - 1 ),   // %1$s = tabulation
            $option['value']['link'],   // %2$s = link (URL)
            $option['value']['title']   // %3$s = title
        );
}

// HTML wrapper for the menu (close)
$this->html[] = '</ul>';

return implode( "\r\n", $this->html );
}
}

code output like this :

 <ul>
   <li><a href="/1.html">1</a>
    <ul class="submenu">
    <li><a href="2.html">2</a>
        <ul class="submenu">
            <li><a href="3.html">3</a>
                <ul class="submenu">
                        <li><a href="4.html">4</a></li>
                    </ul>
            </li>
        </ul>
    </li>
</ul>

And I want somthing like this :

<ul>
   <li><a href="link1">Link1</a>
   <ul>
     <li><a href="link1/link2">Link2</a>
    <ul>
     <li><a href="link1/link2/link3">Link3</a></li>
      </ul>  
   </li>
    </ul>
  </li>
  <li><a href="link4">Link4</a></li>
</ul>

Thank you very much for your help

3
  • @DrakaSAN I got the preceding address to be added to the next link, but this is done as well for the menus with no children by mistake.. Commented Dec 6, 2015 at 16:11
  • Thanks god! I finally made it by myself Commented Dec 7, 2015 at 10:51
  • @user3754884 Can you please put the code how can you get this type of url in nested menu ? Commented Jun 25, 2018 at 10:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.