Home » Technobabble » Including Comments on WordPress Author Pages

Including Comments on WordPress Author Pages

This is a little (hell, VERY) technical–but it might be useful to some, and it seems like my most technical posts get the most attention somehow! 🙂

When running a WordPress site with multiple authors (a class site is a perfect example), sometimes you want to see all the posts by one author gathered together.  WordPress makes this quite easy–you can just go to www.name-of-site/author/authors-username  . Or there is even a sidebar widget to automatically list all the authors names, linked to those pages with all their posts.

But we’ve found that many times, on a class site, a professor (or maybe even visitors to the site like other students) will want to easily review everything that a given student has done.  In other words, they want an author archive page that includes all the posts by that author, and also all the comments that that author has left on posts written by others

This was surprisingly difficult to find a way to accomplish.  There are a couple of plugins that do something like this–but not exactly–and I couldn’t even get the best candidate among them to work at all.

So I decided to get my hands dirty.  When WordPress displays that author’s page, it looks (first) for a file called author.php .  And you can (in a child theme or editing a theme or creating a theme) customize that file to display what you want.

I made a child theme of the Twentyeleven default theme, and put a new author.php file in that child.  The code is already there in the existing author.php to queue the first post, identifying the author, and then run the loop to pull all posts by that author.

I added a small piece of code–of course this has to be enclosed in the usual <?php and ?>

$comment_author = get_the_author_id()

That just pulls the author_id that we’re going to need in the next bit of code to find all the comments by that author.

Then the next bit of code, which took me far too long to figure out how to write, grabs all the comments by that author, and displays them one by one, with each one including a line about when it is was posted, and what post (with a link) it was commenting on. This could all be customized–and yes, this also needs to be enclosed in <?php and ?>

$comments = get_comments(array(user_id=>$comment_author));
foreach($comments as $comment) :
$url = ‘<a href=”‘ . get_permalink($comment->comment_post_ID) . ‘”>’ . get_the_title($comment->comment_post_ID) . ‘</a>’;
echo (‘”‘ . $comment->comment_content . ‘” <br> –( posted on ‘ . get_comment_date(‘M j, Y’) . ‘, commenting on the post ‘ . $url . ‘ )<br /> <br>’);
endforeach;

If you try copying and pasting that code, do be careful about the single and double quotes. I’m not going to bother fixing them all, but they do need to be plain, not curly or “smart” single and double quotation marks. The code is fussy that way!

That’s about it. I also changed the template a bit so that the posts would not be displayed in their entirety, but just as titles linking to the full posts. And I turned off pagination for this page.

An example of it in action, which may not last, since it’s a demo site I use for lots of things, is here.

Hope this is useful for anyone who comes across it!


4 Comments

  1. Very cool, Joe… very useful. Actually, we just rolled this monster out, and I created an author.php page that summarizes what each user has done (drawing upon wp-stats), and then includes a link to a disaggregated index of comments. I may recreate that author.php using what you’ve provided… THANKS!

Leave a comment

Your email address will not be published. Required fields are marked *

css.php
Need help with the Commons? Visit our
help page
Send us a message
Skip to toolbar