FORUMS CLOSED DUE TO SPAM. YOU STILL CAN ADD POST!

[Solved] Sub navigation with identifable first and last items

Feed 3 posts, 3 voices

Avatar
3 posts

I’m using the following code to display the child items on the parent page and the sibling pages. But what I also need to do is identify the first and last items.

I tried using a normal for loop however it completely messed up the page so nothing was displayed at all.

<?php
if ($this->level() > 1) {
  $cur = $this->parent;
} else {
  $cur = $this;
}
if (count($cur->children()) > 0) {
   echo '<ul id="sub-nav">';
   foreach($cur->children() as $menu) { 
     echo '<li' . ((url_start_with($menu->url)) ? ' class="current"': null) . '>'.$menu->link($menu->title, '').'</li>';
   }
   echo '</ul>';
}
?>

(btw how do I get the code box to display, I tried bc. but it didn’t work)
(update use bc.. !)

 
Avatar
1493 posts

Not sure about the first/last, but is this messing up your concatenation?

$menu->link($menu->title, '')

perhaps try it with

$menu->link($menu->title)

??

 
Avatar
8 posts

A simple solution would be to load the children in an array first:

$children = $cur->children();
foreach( $children as $child )
{
  if( $child == $children[0] )
    echo "first";
  if( $child == end( $children ) )
    echo "last";
  //...
}
 
Avatar
3 posts

The solution renz suggested works perfectly.

Thanks :)