Jump to content

Recommended Posts

Posted

Hi,

This a total newbie question, but how can I modify the code below so that a specific css class (.last) is added to the last <li>?

<?php

$homepage = $pages->get("/");

$children = $homepage->children;

$children->prepend($homepage);

foreach($children as $child) {

$class = $child === $page->rootParent ? "class='active'" : '';

echo "<li $class ><a href='{$child->url}'>{$child->title}</a></li>";

}

?>

Posted

I'd do it like this. Add this after assigning value for $class.

$class = $child === $children->children->last() ? $class . " last" : $class;

I'm sure there's a better way to do this, but im on my phone. You get the idea.

Also, you might want to check your code as I think it only displays your first level navs. But that's ok if that's what you're going for.

Have fun with pw!

Posted

Hi Tom Tom & Welcome to the forums.

There are a few ways you could address this. Here's just one possible solution (there are plenty more)...

<?php
$homepage = $pages->get("/");
$children = $homepage->children;
$children->prepend($homepage);
$n = count($children);
foreach($children as $i => $child) {
 $class = "class='";
 if( $child === $page->rootParent ) $class .= "active ";
 if( $i	 == $n - 1)			 $class .= "last";
 $class .= "'";

 echo "<li $class><a href='{$child->url}'>{$child->title}</a></li>";
}
?>

If you are looking to generate a menu-like structure there's a markup module you might want to try as well as looking at doing this via your template file.

Posted

Another one to add to the mix:

$count = count($page->children)-1; 
foreach($page->children as $n => $child) {
 $class = "a$n b" . ($count - $n); 
 echo "<li class='$class'>";
}

Using this method, you can target the first item with class "a0" or the last item with class "b0". You can also target any item from the start or the end by specify "a[n]" from the start, or "b[n]" from the back, where [n] is the zero-based index of the item from the start/end.

  • Like 2
Posted

Hi Tom Tom & Welcome to the forums.

There are a few ways you could address this. Here's just one possible solution (there are plenty more)...

<?php
$homepage = $pages->get("/");
$children = $homepage->children;
$children->prepend($homepage);
$n = count($children);
foreach($children as $i => $child) {
 $class = "class='";
 if( $child === $page->rootParent ) $class .= "active ";
 if( $i	 == $n - 1)			 $class .= "last";
 $class .= "'";

 echo "<li $class><a href='{$child->url}'>{$child->title}</a></li>";
}
?>

If you are looking to generate a menu-like structure there's a markup module you might want to try as well as looking at doing this via your template file.

Thanks for all the responses, I tried the code above and worked perfectly!

Posted

Using this method, you can target the first item with class "a0" or the last item with class "b0". You can also target any item from the start or the end by specify "a[n]" from the start, or "b[n]" from the back, where [n] is the zero-based index of the item from the start/end.

I like this approach, it's more flexible and simpler to program than my example.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...