tags:

views:

62

answers:

1

I'm drawing out a listing of menu items inside a wordpress template (header.php) and need to assign a special className to the last menu item in the list. I'm building the list with this code...

$myposts = get_posts();
foreach($myposts as $post) :?> 
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

I'd like to add this to the last menu item...

<li class="last">...
+1  A: 

You could try this (assuming get_posts() returns an array...)

<?php 
  $myposts = get_posts();
  $last_key = end(array_keys($array));
  foreach($myposts as $key => $post) :
?>
<li <?php if ($key==$last_key) echo 'class="last"'; ?>><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
gnarf