• Hi,
    I have a piece of code which I’m using to add a sequential number to each item in a list. The reason for this is because the graphic designer for the project wants a different background item for everything in the list, and doing this works perfectly for me. However, a problem has arisen as the code was being used for just a one-level list. Now, it’s got two levels but I don’t want the child list items to be included in the count. Does anyone know how I could change this code so that it only counts items in the parent list?

    Here’s my code at the moment:

    $navcount = wp_nav_menu( array(
    								'theme_location' => 'Top Bar',
    								'menu' => 'Main Menu',
    								'depth'      => 2,
    								'container'  => false,
    								'echo'            => false,
    								'menu_class'     => 'nav'
    							) ); 
    
    			$nav_bits = explode('<li ', $navcount);
    			$navcount = ''; $i = 0;
    			foreach($nav_bits as $bits) :
    			if($i==0) { $navcount = $navcount.$bits; }
    			else { $navcount = $navcount.'<li class="item'.$i.'" '.$bits; }
    			$i++;
    			endforeach;
    			echo $navcount;

    Thanks!

Viewing 1 replies (of 1 total)
  • I think this will do what you want:

    $navcount = wp_nav_menu( array(
          'theme_location' => 'Top Bar',
          'menu' => 'Main',
          'depth'      => 2,
          'container'  => false,
          'echo'            => false,
          'menu_class'     => 'nav'
       ) );
    
    $nav_bits = explode('<li ', $navcount);
    
    $i = 0;
    $depth = 0;
    $navcount = array_shift($nav_bits);
    foreach ($nav_bits as $bits) :
       $class = '';
       ++$depth;
       if($depth == 1) :
          ++$i;
          $class = "class='item$i'";
       endif;
       $end_li_count = substr_count($bits, '</li>');
       // echo "$i &nbsp; DEPTH:$depth &nbsp; ENDCOUNT:$end_li_count &nbsp; $class " . htmlentities($bits) . '<br />';
       $depth -= $end_li_count;
       $navcount .= "<li $class $bits";
    endforeach;
    
    echo $navcount;
Viewing 1 replies (of 1 total)
  • The topic ‘Creating Number Count on List’ is closed to new replies.