Hacking WordPress comments onto the front page

For the past week or so, I’ve been working on a new design for Two-Penny Words. Based on the kind of content that’s going on the site, I decided in the design stage to shy away from the generic blog concept and try to fashion more of a magazine style that encouraged reader participation. A large part of the concept is to have comments for a story appear on the front page with the story copy.

Therein lies a problem, however.

WordPress was designed to be a content management system for blogs, which typically have multiple ‘stories’ on the front page at a time. When you stop to think about it, it just doesn’t make sense to have functionality for comments on the front page because of this. Which story would comments apply to? How would you visually try to present comments for five stories on a page? Ten? Beyond having a short list of most recent comments, you don’t.

In my case, however, I’ve configured WP to display one story at most, so front page comments now make sense if I want to have them. I wanted to put them in my sidebar, but the call to the sidebar is outside of the WP Loop, so the variables pertaining to the post were out of scope. At this point, I tried with some success to re-invent the wheel by creating a second WP Loop and refashioning the comments-popup.php in the sidebar.php code, but I was having a strange problem where even though the $comments array was being assigned, the normal WP functions for dealing with comments were coming up empty for $comment after the foreach ($comments as $comment).

Google didn’t yield any insight into the problem, so after a bit of poking around in the /wp-includes directory I’m writing up what I found here in case anybody else is trying to do this, too.

Eventually I found my way to the comment-functions.php file, and right there near the top I found the problem as well as a solution for keeping my sidebar.php file nice and clean. In the comments_template() function, there is an if condition that tests for what kind of page the function is being called from, and it didn’t include a test for is_home(). Adding that solved the problem handily.

I also noticed the function declaration:
function comments_template( $file = '/comments.php' )
Apparently you can supply your own template file as a string to comments_template(), so I copied and pasted the contents of comments.php into comments-home.php and placed a call to comments_template(’/comments-home.php’) in my sidebar.php file, removing all the cruft I hacked in there (which probably would have worked now with the is_home() check in comment-functions.php).

Nice and neat. Plus, now I can easily customize comments-home.php for display on the front page separately from comments.php on the single.php pages.

Just don’t forget to reapply the is_home() customization to comment-functions.php when you upgrade your WP installation.

3 Responses to “Hacking WordPress comments onto the front page”

  1. Anju Says:

    If I didnt already know what you were talking about, I’d be really confused. You’re very sweet. And also very nerdy.

  2. Kenn Says:

    And this is why PHP is not your friend.

    Yeah on making it work though!

  3. Karl Says:

    can you show the code, i have been wanting to do something like that for a while!

Leave a Reply