FAQ: Showing recent Entries in the Sidebar

Question

I want to have the most recently published entries in the sidebar. My code works most of the time. However, the category archive and the date-based archive only show some of these entries. How can I show all entries?

Answer

You have probably saved the code for the sidebar in a module template, and include that template from the Main Index Template, the Category Template, and the Date-based Template. Alternatively, you simply copied the code for the sidebar into the three templates (if so, please read Organize your Templates with MTInclude for learning an important feature of Movable Type).

Your code for showing the ten most recently published entries might be as follows.

<h2>Recently Published</h2>
<ul>
<MTEntries lastn="10">
<li><a href="<MTEntryLink>"><MTEntryTitle></a></li>
</MTEntries>
</ul>

If executing these lines in the Main Index, anything will work perfectly. The ten most recently published entries will appear in an unordered list, each being a link to the entry.

However, if these lines belong to a Category Archive, the ten most recently published entries for the current category will be listed.

This behaviour is by design. Many tags work with respect to the context, in which they are used. If a category archive is created, MTEntries defaults to the list of entries for that category. The same way, if a monthly date-based archive is created, MTEntries defaults to the current month.

Most of the time, this behaviour is reasonable. However, our wish to have a list of the most recently published entries independent of any criteria cannot be solved easily. There is no attribute for the MTEntries tag giving the full list of entries.

The Solution

The solution, which I am going to show, is based on the following...

If during a rebuild, a category or a date-based archive is created, the list of entries will already have been collected and saved into a separate file. Then, the template for the category archive will not build the list itself - it will simply include the prepared file.

We will use an Index Template for the separate file, containing nothing more than a couple of template lines for creating the list of entries. Its sole purpose is to be included by other templates. It will never be shown to the user on its own.

Of course, it is important that the list of entries is created before any of the templates tries to include that file. As Movable Type always creates index files first, this is guaranteed.

So let us start: open your Movable Type project. Click on "Templates" in the left menu bar. The list of templates will appear. Click on "Create new index template". Enter "most-recent-entries" as the Template Name and "most-recent-entries.html" as the Output File. The Template Body should be as follows (same as above)...

<h2>Recently Published</h2>
<ul>
<MTEntries lastn="10">
<li><a href="<MTEntryLink>"><MTEntryTitle></a></li>
</MTEntries>
</ul>

Save the template. Now go back to the list of templates, and select the Category Archive Template. Modify the template source by inserting the following tag...

<MTInclude file="your_path\most-recent-entries.html">

The absolute path must be the path to your output directory. This is the same path that your entered as the "Local Site Path" in your weblog configuration.

Rebuild your site. Now, also the category archive will show the list of the most recently published entries.

Details you might want to change

The name of the added index template is completely up to you. You can name it, as you like.

When specifying the output file for the template, you can also give an absolute path. As we have given just the filename, the generated file is placed into the default output directory. For example, you might place the generated file into C:\DATA\MT\most-recent-entries.html. Of course, the include statement has to be changed accordingly.

You might also give another file extension. You will often read "inc" for such a file (inc = include). Giving this file another extension has the advantage that your internet server will not serve this file. If you follow the above, a file "most-recent-entries.htm" will be in your main directory. Any user, knowing about its existence, would be able to open that file.

mgs

Feedback is welcome!

What do you think about this entry? Was it interesting or boring? I would like to hear your comments. If the text was helpful, please consider setting a link to http://www.movable-type-weblog.com/.

No spam please!

For protecting this weblog I have installed the MT-Approval Plugin. You have to view a new comment in preview mode, before it is saved on the server. Moreover, I will view your comment manually, before it is published. You can find more information on the subject in the entry Weblog Spamming Basics.

With an active TypeKey session, your comment will be published immediately.

Post a new comment

TypeKey has temporarily been disabled at this location. Please create your comment without using TypeKey or log in from the preview dialog.




Remember Me?


Comment

LI | October 17, 2005 04:54 PM

Save the template. Now go back to the list of templates, and select the Category Archive Template. Modify the template source by inserting the following tag...

This would be more helpful if you had mentioned where exactly in the code it should be inserted.

Comment

Michael G. Schneider | October 17, 2005 06:38 PM

There is not one correct location where the MTInclude has to be inserted. It totally depends on your weblog.

Where ever the list of entries is to appear, there insert the MTInclude statement.

Comment

Harry | October 29, 2005 07:39 PM

great fix, thanks for this saved me a lot of greif trying to work it out and learnt about includes for MT. It was easy to work out, once I got the site path fixed up, in my case I publish to the root on a virtual server so didn't need a file path.

One thing tho - the sample code above is missing the closing a-tag. I think it should be:

<li><a href="<MTEntryLink>"><MTEntryTitle></a></li>

Comment

Michael G. Schneider | October 29, 2005 08:55 PM

Harry, thanks a lot for pointing out the missing tag. I corrected it immediately.

Comment

Kari Chisholm | March 12, 2006 09:51 PM

Awesome! This was a lifesaver. I'm using it for both a "most recent entries" list on every page, but also for a pulldown of all categories -- even on category pages.

Comment

Tim [TypeKey Profile Page] | April 25, 2008 10:30 AM

Thanks so much. It took me awhile to get it to work right, but that's exactly what I was looking for!