user-pic

Add a CSS class every nth entry

Vote 0 Votes

I'm trying to output a series of entries as a list but I need to be able to mark each third one with a special CSS class. I noted that there's something that does exactly what I need for outputting assets but not entries. i've been stumbling through trying to get something working for entries for a couple hours now with no luck and am hoping someone can help.

The gist of what I'm looking to produce is this:

<ul>
  <li>Entry one</li>
  <li>Entry two</li>
  <li class="last">Entry three</li>
  <li>Entry four</li>
  <li>Entry five</li>
  <li class="last">Entry six</li>
  <li>Entry seven</li>
</ul>

This is simple to do with assets using the mt:FirstAssetInRow or mt:LastAssetInRow tags with the "assets_per_row" argument on the mt:Assets tag but nothing like that exists for entries :(

Reported on Movable Type 4.3

4 Replies

| Add a Reply
  • The general trend is moving away from things like EntriesHeader, because such constructs are specific to the point of being limiting if you want to do anything that's just slightly different. Those asset tags work out for you in this particular situation(when you're using assets), but that's not really what they're for. Anyway, back to the question.

    Most of the major loops now have a set of meta-variables available to them. You're currently interested in __counter__ Odd and even are handy shortcuts for on/off situations, but since you're working with threes, you have to do a bit of the work yourself. For that you want the If tag. Then:

    <mt:entries>
    <li<mt:if name="__counter__" op="mod" value="3" eq="0"> class="last"</mt:if>>Blah</li>
    </mt:entries>
    

    Although, you bringing up the asset tags is making me wonder exactly what you're doing. Are you just marking off every third item, or is the end goal a grid? Because there's a plugin that addresses that directly, and has several more options that you might find useful.

  • Thanks so much Su - works like a charm.

    I had a feeling there had to be a simple way to do what I needed. I think I was just over complicating things and not thinking though the options available or misunderstanding how some of the meta variables work.

    The end goal visually was something resembling a grid layout (but not using a table). I had looked at the MTGrid plugin but that seemed like overkill for what I needed.

    • Just as a quick note on this in case you do end up needing any of that extra stuff, the Grid plugin makes no assumptions at all about the markup. All it does is provide the structures for dealing with your content bits in a grid-like format.

  • MTGrid is fantastic even for CSS layouts, but it doesn't provide the ability to do complex array counting operations as Su has demonstrated above. For example, in some grid layouts you may want to apply CSS formatting to additional cells other than just the left/first and right/last cells in a row. In my experiments with MTGrid, this isn't always possible depending on what one is trying to do.

    That said, some combination of MT's array counting functions and MTGrid could work beautifully.

Add a Reply

If you need to share template code, replace all the "<" signs with "&lt;" or use this utility.

Forum Groups

1773 6162

Last Topic: Excluding categories from blog by kholechek on Feb 9, 2012

86 302

Last Topic: website entries by masoud on Oct 26, 2011

1429 5077

Last Topic: What apocalypse hit this community in the middle of 2011? by 75th on Feb 10, 2012

695 2910

Last Topic: Insert Image / File Fails by Russ Miller on Feb 10, 2012

84 291

Last Topic: How to have some other characters in entry basename automatically written by Afshin Haghighatnia on Dec 22, 2011

173 737

Last Topic: About the MT version stated in HTML source by Alex E. Schneider on Feb 7, 2012

190 567

Last Topic: Analytics Reporting by michael webster on Feb 5, 2012

48 210

Last Topic: An idea and also a request by Afshin Haghighatnia on Jun 29, 2011

64 246

Last Topic: jQuery in MT 5.1 still at 1.4 - why? by perlmonkey on May 25, 2011

code.sixapart.com

137 478

Last Topic: Getting a thumbnail with xpath by Peter on Mar 13, 2011

222 720

Last Topic: Custom Field for Asset Not Appearing by android on Feb 9, 2012