<MTEntries> tag in Movable Type has a
attribute, which lists the 'N' most recently commented on entries. However, this attribute
does not work in category archive pages - MT ignores the attribute in that case and will
list every entry in the category.
At first, I thought this was a bug, but after doing some searching through the
MT Forums, I discovered that the
recently_commented_on attribute has always behaved this way - it was never
designed to work in a category archive.
The typical solution is to use a separate index template containing the
recently_commented_on attribute, and use a PHP or SSI include to insert
the recently commented on entry list into a category archive page. This weblog does
not use PHP, and only my blog's main page is set up for SSI, so I preferred a solution
that did not require PHP or SSI.
Using Brad Choate's SQL plugin, I created an SQL query that lists recently commented on entries for whatever category is in context on a category archive page.
Here's the code I'm using in my category archive templates:
<MTSQLEntries query="SELECT entry_id, MAX(comment_created_on) as comment_created_on FROM mt_comment LEFT JOIN mt_entry ON comment_entry_id = entry_id LEFT JOIN mt_placement ON entry_id = placement_entry_id LEFT JOIN mt_category ON placement_category_id = category_id WHERE entry_blog_id = [MTBlogID] AND entry_status = 2 AND comment_visible = 1 AND category_label = '[MTArchiveCategory encode_js='1']' GROUP BY entry_id ORDER BY comment_created_on DESC LIMIT 5"> <p><a href="<$MTEntryPermalink$>"><$MTEntryTitle$></a><br /> <MTComments lastn="1" sort_order="descend"> <$MTCommentAuthorLink show_email="0"$>: <$MTCommentBody convert_breaks="0" remove_html="1" trim_to="50"$>... [<a href="<MTCommentEntry><$MTEntryPermalink$></MTCommentEntry>#c<$MTCommentID$>" title="go directly to comment">go</a>]</p> </MTComments> </MTSQLEntries>
This query will automatically 1) select entries from the current weblog, 2) select only published entries, 3) select recently commented on entries based only on approved comments, 4) restrict the list of entries to the current archive category.
To change the number of entries returned, change the
LIMIT 5 (last line of the query)
to whatever number of recently commented on entries you wish to display.
<MTArchiveCategory> tag in the query string includes the 'encode_js' attribute
to escape (with a backslash) any quotes or apostrophes that appear in the category name.
The above code is intended to work with MT3D. If you wish to use this code with MT 2.6x,
delete the line
AND comment_visible = 1 from the query. (MT 2.6x does not have this
field in its database.)
Addendum: While I was posting this tip in the MT Forums, I realized that the link
to the comments used a bookmark to the individual comment (
people may or may not have.
To add your own individual comment bookmarks, add the following right after the <MTComments> tag in whatever template you're using to display comments (Individual Entry Archive or Comment Listing):
The bookmark can also be changed to just "
#comments" - the link will then take the
reader to the beginning of the comments.
Update 21-Jun-2004: Replaced "<" and ">" in nested MT tags in query string with "[" and "]" so query is easier to read and understand.