- This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
Viewing 7 posts - 1 through 7 (of 7 total)
- The forum ‘Back End’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
Hey Ya’ll,
I’ve got an unordered list displaying my pages in The Loop. Each list item has a margin-right of 20px and I need to rid the right margin off every fourth item in that list. Now, normally I’d use a class of “last” on the fourth item of the list and set the margin to 0. Since I’m doing this in The Loop how would I achieve something like this?
This is how my code looks at the moment:
">
I was thinking of adding to the class of the list item. This works but adds a lot of unnecessary code. I’d like every fourth item to have a class of last, is this possible?
Any help would be appreciated, thanks in advance.
UPDATE: Solved it, for those interested:
Put this in your functions.php file:
function switchClass($n, $i, $prefix = '') {
$output = '';
if (is_numeric($n)) {
if ($i % $n == 0) {
$output = ' '.$prefix.'last';
}
}
return $output;
}
This is your loop. Here it’s echoing the class “first” on every fourth list item (see line 4).
For more info on this implementation check out codesnipp.it.
Pseudo-elements with CSS.
You’re looking for :last-child
It looks like you’ve found a pretty good solution, but since you’ll probably be slapping on some JS to help IE anyways, might as well go the CSS3 route (at least that’s what I would do).
Here’s a pretty good looking list of browser support:
Cheers @TheDoc, I’ll give it a try. Any idea on how I’d make my sub-pages loop in alphabetical order?
Hi. Sweet solution. works perfectly. thanks for sharing