Handling Multiple Posts on the Same Day in Movable Type

closePlease note: This post was published over a year ago, so please be aware that its content may not be quite so accurate anymore. Also, the format of the site has changed since it was published, so please excuse any formatting issues.

The previous version of ALLO (v4) was designed to display only the most recent post, regardless of how many posts had been written that day. It was a decision I made for aesthetic reasons, but because of it, I had the tendency to put off posts if I had already posted once that day. The problem was that I would then forget to post the next day (or something more timely would come along), and after a couple of days, I had forgotten what it was I wanted to write about in the first place. This was especially common on Fridays, when the Friday Funday post needed to be in the spotlight.

One of my goals for v5 (the current version as of this post), was to be able to write multiple posts in a single day. This goal had two parts, only one of which had been heretofore accomplished. The first (and easiest) part of achieving the goal was to show more than one post on the front page. This also eliminated the need for a “5 Most Recent Entries” list in the navbar, which allowed me to add the Dailies and not feel like a taxidermist trying to fill a rat with the entire Gobi Desert (bonus points for getting the reference (I’m looking at you, Brynna)).

The second part of the goal proved to be more difficult, but nowhere near as difficult as I had thought it would be. As it turns out, all I needed was a little direction and a better understanding of some of the less commonly used Movable Type tags.

Each entry on the blog has a date box next to it, which is cool and all, but I thought it looked kind of dopey for there to be several date boxes that all had the same date on them. The ideal situation would be one date box per day, regardless of how many posts there were for that day. I had no idea how to accomplish this, or even how to search for how to accomplish it, but after Googling a few different phrases, I finally found an extremely useful article by Brad Choate (Brad actually contributed the MTElse tag, which I’m going to talk about).

Brad’s solution was to use three Movable Type tags that I hadn’t had much (read: any) experience with. Those tags are MTDateHeader, MTDateFooter, and MTElse. The sample code on his site was intimidating at first, but once I understood what was going on (with a little help from the Movable Type website), I was able to rewrite it in a way that made the code much less intimidating:

<MTDateHeader>

  <MTDateFooter>

    Header is true, Footer is true

    Insert Date Box

  </MTDateFooter>

  <MTDateFooter>

    <MTElse>

      Header is true, Footer is false

      Insert Date Box

    </MTElse>

  </MTDateFooter>

  <MTElse>

    <MTDateFooter>

      Header is false, Footer is true

      Insert Nothing

    </MTDateFooter>

    <MTDateFooter>

      <MTElse>

        Header is false, Footer is false

        Insert Nothing

      </MTElse>

    </MTDateFooter>

  </MTElse>

</MTDateHeader>

MTDateHeader and MTDateFooter are conditionals, meaning the code inside the tags is only executed when the condition is met. MTDateHeader signals the first post of a given date, while MTDateFooter signals the last post of that date. In this case, the code I want to execute is represented by the statement, “Insert Date Box”. Where it says, “Insert Nothing”, there is literally nothing there in my actual code. Also, while this is obvious to me, it bears pointing out, just in case: The “Header ture, Footer true” and the rest of the statements like it are not actually part of the code, they’re just there so that you understand the conditions in each section.

The key here is understanding how MTElse works. MTElse must be put inside of another Movable Type tag, and when you do that, it basically says, “this means the opposite.” So in the first section, the logic works out like this:

<MTDateHeader>

  <MTDateFooter>

    If the header is true and the footer is true, return true.

    In plain English: “Do this when the header is true and the footer is true.”

  </MTDateFooter>

  <MTDateFooter>

    <MTElse>

      If the header is true and the footer is false, return true.

      In plain English: “Do this when the header is true and the footer is false.”

    </MTElse>

  </MTDateFooter>

</MTDateHeader>

As you can see, adding MTElse makes it so that the code will execute even when MTDateFooter is false, as long as MTDateHeader is true. If I didn’t have the second section (which is exactly the same as the first section, except for the addition of the MTElse tag), then the plain English would go something like this: “Do this when the header is true and the footer is true, but if the header is true and footer is false, don’t do it.”

Remember how MTDateHeader and MTDateFooter signal the first and last posts respectively? If we look at the first section of the example above, they’re both true, which would indicate that the entry in question is both the first and last entry of the date (in other words, it’s the only entry for that date). In the second section, the header is true, indicating that the entry is the first one, but the footer is false, indicating that there are additional entries for that date.

One last thing I want to point out about this code is that it should be placed where you want the conditional code to be executed. I know that sounds daft, but in Brad’s post, he says that the code should go just before the closing MTEntries tag. He’s right, but only for the example he’s giving. In my case, I want the code to go where the date box code should go, which, as it turns out, is just after the opening MTEntries tag.

Hopefully this helps someone else out there. Movable Type is a great program, but sometimes it can be hard to find solutions to problems, especially when the problem seems kind of obscure, or you aren’t sure what words to use to search for the answer.

And, of course, now that I have achieved my goal, I plan to start posting multiple entries from time to time, so now you have another reason to check my blog several times a day!

0 People like this. Be the first!

Leave a Reply