Most Commented on Entries

UPDATE 3.29.04
Lynda has a updated version of this script. I’ll leave the content of the post up - but go to her site for the most recent version of the script.

——————————————-

I went looking to see if there was a way to list the most commented on entries within Moveable Type. I know this is a standard Greymatter feature, but I couldn’t figure anything out for Moveable Type. Granted, I may not have looked hard enough.

Here is a simple PHP solution. I’m just going to copy and paste the code, it’s quite easily customizable if you know what’s going on. When I get back I might detail this post a little more.

If you know of a way to do this automatically within Moveable Type, please let me know.

First you will want to create a new index template. Give it any name you’d like, but remember this name for later on. For demonstration purposes, we will pretend the file is named comments.inc.

Copy and paste the following code into the template body:

<?php

<MTEntries lastn=”99999″>$comments[<$MTEntryID$>] = <$MTEntryCommentCount$>;
</MTEntries>

<MTEntries lastn=”99999″>$entryinfo[<$MTEntryID$>] = “<$MTEntryTitle encode_html=”1″ encode_php=”qq”$>|<$MTEntryLink$>|<$MTEntryDate format=”%m.%d”$>”;
</MTEntries>

?>

You can change the MTEntryDate format to however you wish it to display. Save it and rebuild the index files.

Then, open a text editor and paste the following into that, changing only the first two lines per your preference:

<?php

# This is the path to the comments.inc:
include(”/home/user/public_html/blog/comments.inc”);

# This is the number of results you want to show:
$results = 5;

array_multisort($comments, SORT_DESC, $entryinfo);
$i = 0;
do {
$entries = explode(”|”, $entryinfo[$i]);
$entry_title = $entries[0];
$entry_link = $entries[1];
$entry_date = $entries[2];

echo “<b>$comments[$i] comments</b>: <a href=\”$entry_link\”>$entry_title</a> ($entry_date)<br/>”;

$i++;
} while ($i<$results);

?>

Save this file under any name you wish, with a PHP extension. Example, comments.php.

You should now be able to include this file where you’d like on your blog using the following code:

<?php include(”/home/user/public_html/blog/comments.php”); ?>

Make sure to change the path to correctly reflect the path to your comments.php

30 Responses to “Most Commented on Entries”

  1. Jennifer Says:

    Cool! I like it!! Maybe when I have time (HAHAHAHA!!!) ;-) I’ll implement that here!

  2. Jade Says:

    Oh, muchy likey. Once I switch my journal over to MT, then I will do this. Eventually.

  3. dvg Says:

    Wow! This kicks boo-tay. I’ve been trying to do something like this for a while now. I got it working on my main page in about 3 minutes. It adds a bit to the rebuild time, but not too much. Very cool! And I have never tried anything with PHP before, so I was quite thrilled to have it up and running so quickly. Thanks, girls!

  4. pluramon Says:

    Nice idea.

    PHP is my friend. Or would be if I could integrate it within MT. Any day-zero PHP and MT tutorials
    out there? Nothing I add PHPwise works, including the great sounding script above. Rebuild for the eg above chokes thus:

    Template ‘comments.inc’ does not have an Output File

    what gives?

    thanks in advance….

  5. Lynda Says:

    Sounds like you didn’t choose a file name for comments.inc In the box marked “Output file:” enter… comments.inc

  6. pluramon Says:

    Thanks for your help lynda… Partial success ( !! ), I guess: php now doing …something:

    comments: ()
    comments: ()
    comments: ()
    comments: ()
    comments: ()

    hmm..not very informative but I’ve only got a test blog up with 4 articles in it and only two are commented; would that be the problem?

  7. David Melle Says:

    Hello Goddesses,

    Thanks for the tip!

    I was looking for a way to do this and it literally took me 5 minutes to implement it.

    Thanks you very much for sharing your scripts and intelligence!

    Bye,

    David Melle
    dmelle@factsofisrael.com
    http://www.FactsOfIsrael.com

  8. Simply Sara Says:

    Love it, totally, wonderful!

    Is there a way to add the year though? Thanks for everything y’all do~
    :)

  9. Simply Sara Says:

    Never mind, found it, silly me. :)

  10. Al-Muhajabah Says:

    This is a great little trick. I would like to also display the TopicIcon for each of the entries. How would I modify the PHP code to include this in the entry info and then display it?

  11. iced glare Says:

    thanks girlie I got it !

  12. yosshi Says:

    hello,
    I was using this script and everything was going well before.
    Now, because of reasons i’ve never known, the script returns an error message, such as:
    Warning: Argument 1 to array_multisort() is expected to be an array or a sort flag in /home/***/***/comments.php on line 6.

    what can I do about this?

    thanks.

  13. yosshi Says:

    sorry, it’s all my mistake.
    I read manual again and did it. it works well again.

    sorry.

  14. Mikel Says:

    Hi!
    Thanks for this trick but i can’t get it running. I’ve donne everything but I can’t see anything in my main index.
    Do somebody knows what is happening?

  15. Jennifer Says:

    I wouldn’t mind using this with a second blog.

    I thought that all I’d have to do is, in the comments.inc file, use a second set of the same commands, enclosed in MTOtherBlog tags (I have the plugin installed).

    This didn’t seem to work. The comments.inc file lists only the first blog, not the second.

    I even tried making two comments.inc files and pulling them both into the comments.php. That didn’t do anything useful at all, though both .inc files showed their correct respective data.

    Am I missing something here? Is there a modification I need to make to this somewhere else? Or is this just not a real possibility?

  16. Adam Says:

    If you have Brad Choate’s MTSQL installed, you can use this little bit of SQL to fetch the five most commented on stories:

    SELECT comment_entry_id AS entry_id, COUNT(*) AS count FROM mt_comment WHERE comment_blog_id = 1 GROUP BY entry_id ORDER BY count DESC LIMIT 5;

    (Note that comment_blog_id might differ slightly in your setup.)

  17. James Says:

    Can this feature be used if my archives in html instead of php? Thanks!

  18. Jennifer Says:

    Yes, but you would need to get your server to process your .html/.htm pages as if they were .php. See this post on how to do that.

  19. matt Says:

    I’ve done everything, step by step. I’ve created the htaccess file because I use html. However nothing happens, no error message, no results, nothing. Help…

  20. Mindy Says:

    I have installed everything according to the directions, but cannot get anything to show in the output file. If I view the output file in edit mode, I see all of the comment data, but nothing shows when I view the file. I have reviewed all of the working add-ons to see where the error is, but cannot find anything amiss. Any suggestions?

    Thank YOU!!

  21. Jennifer Says:

    Can you provide a link to the files you’ve created?

  22. Lynda Says:

    Here is an updated version of this script using SQL and trackback information.

  23. Jennifer Says:

    Cool! Thanks Lynda. I’ll update the main post with your link :)

  24. SV Says:

    Thanks a million for the code. Now all I need to do is beautify.

  25. jim Says:

    Hello,

    I am trying to use your connect.php but not having any success. The following is a link to the message i am getting. Any help would be greatly appreciated. Thank you.
    http://www.advancinginsights.com/testblog-1/most_commented.php

  26. SV Says:

    I used this code and it worked like a song. And sent my comment with thanks too. Now mblog.com (where my blog is) decided not to allow php. Can you help me with pavascript code to do the same?? Please! Please!!

  27. brian Says:

    I can’t get the <?php include(”http://www.bingwalker.com/blog/comments.php”); ?> to show anything on my page. My .inc file is here:
    http://www.bingwalker.com/comments.inc
    and my php file is here:
    http://www.bingwalker.com/blog/comments.php

    Any help on the error message with .php?

  28. brian Says:

    I can’t get the “php include” to output anything….. :(

  29. ***Dave Does the Blog Says:
    Commentary
    Les links to today’s Dork Tower on a topic important to most bloggers. Go ahead and read it … ……

  30. Jim Steed Says:

    Before I saw your page, I wrote a MT Plugin that seems to do the same thing. It is available at
    http://www.fakejazz.com/fake/archives/2005/02/movable_type_pl.php if anyone wants to try it out. Since it is a normal plugin, you wouldn’t need PHP and it is context-sensitive to MTCategories, etc. I am using it to show the top 5 most commented on entries in each category. (Or maybe I should say “will be using it” since I just switched from hand-coding everything to MT and don’t have many comments yet.)