title: Check how many times <li> tag is printed, if more times create new row
body: <p>I have created wp menu in administration panel named "Main menu", then I am trying to retrieve it in header. I get items but not able to get child below parent.</p>
  
  <p>In my HTML template, menu structure is like this:</p>
  
  <pre><code>&lt;ul class="rd-navbar-nav"&gt;
      &lt;li class="active"&gt;&lt;a href="./"&gt;Home&lt;/a&gt;  &lt;/li&gt;
      &lt;li&gt;&lt;a href="#"&gt;About us&lt;/a&gt; 
  
      &lt;li&gt;&lt;a href="#"&gt;Products&lt;/a&gt; 
      &lt;ul class="rd-navbar-megamenu"&gt; 
          &lt;li&gt;
              &lt;ul class="rd-megamenu-list"&gt;
                  &lt;li&gt;&lt;a href="product-catalog.html"&gt;Product 1&lt;/a&gt;&lt;/li&gt; 
                  &lt;li&gt;&lt;a href="product-catalog.html"&gt;Product 1&lt;/a&gt;&lt;/li&gt; 
                  &lt;li&gt;&lt;a href="product-catalog.html"&gt;Product 1&lt;/a&gt;&lt;/li&gt; 
              &lt;/ul&gt;
          &lt;/li&gt;
      &lt;/ul&gt;
      &lt;li&gt;&lt;a href="#features"&gt;Services&lt;/a&gt; 
      &lt;ul class="rd-navbar-megamenu"&gt;   
          &lt;li&gt; 
              &lt;ul class="rd-megamenu-list"&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 1&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 2&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 3/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 4&lt;/a&gt;&lt;/li&gt;          
              &lt;/ul&gt;
          &lt;/li&gt; 
          &lt;li&gt; 
              &lt;ul class="rd-megamenu-list"&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 5&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 6&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 7&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 8&lt;/a&gt;&lt;/li&gt;
              &lt;/ul&gt;
          &lt;/li&gt;
  
          &lt;li&gt;
             &lt;!--&lt;p class="rd-megamenu-header"&gt;Samouklapajući limovi&lt;/p&gt;--&gt;
            &lt;ul class="rd-megamenu-list"&gt;
              &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 5&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 6&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 7&lt;/a&gt;&lt;/li&gt;
                  &lt;li&gt;&lt;a href="classic-news.html"&gt;Service 8&lt;/a&gt;&lt;/li&gt;
            &lt;/ul&gt;
          &lt;/li&gt;
      &lt;/ul&gt;
      &lt;li&gt;&lt;a href="#"&gt;News&lt;/a&gt;
      &lt;li&gt;&lt;a href="careers.html"&gt;Careers&lt;/a&gt;&lt;/li&gt;
  
      &lt;li&gt;&lt;a href="contacts.html"&gt;Content&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>in header.php I retreive menu that is created in WP admin panel. It is organized well but the only trouble I have is with printing <li> tag. I want to create new row of block below if four <li> are printed</p>
  
  <pre><code>&lt;li&gt; // Here I need to count how many times is printed, if more than 4 then I shoud create new row
                    &lt;ul class="rd-megamenu-list"&gt;
                      &lt;a href="&lt;?php echo $link; ?&gt;" class="title"&gt;&lt;?php echo $title; ?&gt;&lt;/a&gt;
                    &lt;/ul&gt;
                  &lt;/li&gt;
  
  
  
     &lt;ul class="rd-navbar-nav"&gt;
      &lt;?php
      $count = 0;
      $submenu = false;
      foreach( $menuitems as $item ):
          $link = $item-&gt;url;
          $title = $item-&gt;title;
          // item does not have a parent so menu_item_parent equals 0 (false)
          if ( !$item-&gt;menu_item_parent ):
          // save this id for later comparison with sub-menu items
          $parent_id = $item-&gt;ID;
      ?&gt;
  
      &lt;li&gt;
      &lt;a href="&lt;?php echo $link; ?&gt;" class="title"&gt;
          &lt;?php echo $title; ?&gt;
      &lt;/a&gt;
  &lt;?php endif; ?&gt;
  
      &lt;?php if ( $parent_id == $item-&gt;menu_item_parent ): ?&gt;
  
          &lt;?php if ( !$submenu ): $submenu = true; ?&gt;
          &lt;ul class="rd-navbar-megamenu"&gt;
          &lt;?php endif;?&gt;
              &lt;li&gt; // Here I need to count how many times is printed, if more than 4 then I shoud create new row
                &lt;ul class="rd-megamenu-list"&gt;
                  &lt;a href="&lt;?php echo $link; ?&gt;" class="title"&gt;&lt;?php echo $title; ?&gt;&lt;/a&gt;
                &lt;/ul&gt;
              &lt;/li&gt;
             &lt;?php if ( $menuitems[ $count + 1 ]-&gt;menu_item_parent != $parent_id &amp;&amp; $submenu ): ?&gt;
          &lt;/ul&gt;
          &lt;?php $submenu = false; endif; ?&gt;
  
      &lt;?php endif; ?&gt;
  
  &lt;?php if ( $menuitems[ $count + 1 ]-&gt;menu_item_parent != $parent_id ): ?&gt;
  &lt;/li&gt;
  &lt;?php $submenu = false; endif; ?&gt;
  </code></pre>
  
  
  
  <p></p>
  
  <p>Any kind of advice is helpful</p>