Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Using “Last” Class In The Loop (WordPress)

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #32968
    realph
    Participant

    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.

    #74498
    TheDoc
    Member

    Pseudo-elements with CSS.

    You’re looking for :last-child

    https://css-tricks.com/pseudo-class-selectors/

    #74488
    realph
    Participant

    @TheDoc I was under the impression IE doesn’t support this.

    #74491
    TheDoc
    Member

    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:

    http://kimblim.dk/css-tests/selectors/

    #74461
    realph
    Participant

    Cheers @TheDoc, I’ll give it a try. Any idea on how I’d make my sub-pages loop in alphabetical order?

    #74463
    realph
    Participant

    @TheDoc, Don’t worry. Found it:

    'orderby=title&order=ASC'
    #89221
    mycueb
    Member

    Hi. Sweet solution. works perfectly. thanks for sharing

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.