Recently Commented On Entries in Category Archives

| 2 TrackBacks

The <MTEntries> tag in Movable Type has a "recently_commented_on" 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 <MTEntries> tag and 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.

The <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.)

You can see this code in action on any of my category pages or category indexes (Blogging category, Blogging index).

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 (#c<$MTCommentID$>), which 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):

<a id="c<$MTCommentID$>"></a>

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 "&lt;" and "&gt;" in nested MT tags in query string with "[" and "]" so query is easier to read and understand.

2 TrackBacks

Recent Comments from Learning Movable Type on June 11, 2004 7:26 PM

Adding a Recent Comments section to your weblog is pretty easy and straightforward.... Read More

I've made a couple of changes to Carpe Bonum's Movable Type template files. First, I have added sidebars to all the Archive page templates. Readers arriving via permalinks to individual post archive pages will now see the same sidebar as... Read More

About this Entry

This page contains a single entry by TweezerMan published on June 9, 2004 4:36 AM.

Senate bill prohibiting banks from charging fees to cash paychecks approved was the previous entry in this blog.

mBlog Users - Beware! is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 6.1.2

MT-Vampire plugin

web hosting by TotalChoice Hosting

Valid XHTML 1.0!
Valid CSS!