scriptygoddess

15 Jan, 2009

WordPress wp_list_comments()

Posted by: Jennifer In: Bookmarks

I haven't done too much with WordPress 2.7 until very recently. I was really surprised to see the new way of doing comments. The good news is that wp_list_comments() will list all comments, with threading, and nested replies, all by it's little self. You can turn the nested replies thing on/off from the Settings->Discussion page in the admin.

The bad news is that it spits out all the HTML, whether you like its HTML or not. I was reading on the forums the point of doing it this way was to make everyone control the "look" using styles and you "shouldn't need to change the HTML". What you "shouldn't" need to do has been the argument in favor of most issues I have with WordPress. (Don't get me wrong I do love it.) If all anyone needed to do was just change stylesheets, then everyone's HTML would look the same, because we're all doing the same thing, right? Well, we're not. Half the time I use WordPress for standard blogs – where – sure the "default" HTML is fine and I'll make it work from the styles. The other half of the time, I'm twisting WordPress to do all kinds of funky sites. Following this path of "you shouldn't need to do that" is going to start making that flexibility disappear. Applications "shouldn't" need to spit out HTML. I certainly support WordPress' idea of making it easier to use for the common person – but workarounds should always exist for those of that need a more advanced level of control. On the forums, I saw a perfect, simple reason why someone might want to change the HTML…What if I don't want it to say "blahblah says:". wp_comment_list should include parameters to change some of the hardcoded text like that…Okay. Stepping off my soapbox now. :)

Thankfully, there IS a way around it. I wish this page had more information, but it's enough to get started at least. On the wordpress codex – you can see the wp_list_comments() function explanation. (Are the three parameters the only ones wp_list_comments() takes?)

Ironic that the first thing they show you is how to bypass the "auto html dump" the function does… so if you want to customize the HTML, call the function like this:

<ul class="commentlist">
<?php wp_list_comments('callback=mytheme_comment'); ?>
</ul>

Then create a functions.php file in your theme directory and use this as a start to customize the HTML of the output:

function mytheme_comment($comment, $args, $depth) {
$GLOBALS['comment'] = $comment; ?>
<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
<div id="comment-<?php comment_ID(); ?>">
<div class="comment-author vcard">
<?php echo get_avatar($comment,$size='48'); ?>
<?php printf(__('<cite class="fn">%s</cite> <span class="says">says:</span>'), get_comment_author_link()) ?>
</div>
<?php if ($comment->comment_approved == '0') : ?>
<em><?php _e('Your comment is awaiting moderation.') ?></em>
<br />
<?php endif; ?>
<div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>"><?php printf(__('%1$s at %2$s'), get_comment_date(), get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),' ',’’) ?></div>
<?php comment_text() ?>
<div class="reply">
<?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
</div>
</div>
<?php
}

There is a note that says there is no ending "li" tag because wordpress will automatically add that itself. (WHY OH WHY?!) I assume if you don't want it to do comments as list items and instead want divs (and therefore end with "/div" instead of "/li"), then you'd change the call to wp_list_comment to:
<div class="commentlist">
<?php wp_list_comments('style=div&callback=mytheme_comment'); ?>
</div>

There is also a comment on the "callback" parameter on the codex: "Use to customize comments display for extreme changes to the HTML layout. Not recommended." Ah. I feel like such a rebel.

A few side notes:

If you're going to use comment threading, paging, etc then this page has some useful info on it on some important things you'll need in your theme files.

And if you are going to keep the default HTML – then you'll probably need to know WHAT STYLES you need to work with! Here is the list.

This page also has some comment styling information on it.

Here is information on the new way to separate pings from comments.

22 Responses to "WordPress wp_list_comments()"

1 | Kim Woodbridge

January 17th, 2009 at 7:07 am

Avatar

Thanks for this! Last night I decided to make my comments threaded in 2.7 and then discovered it was going to be more complicated than I anticipated since I have a very customized comment template. Hopefully this will assist me when I try again.

2 | scriptygoddess » Joomla vs Wordpress

January 24th, 2009 at 3:56 pm

Avatar

[…]         « WordPress wp_list_comments() […]

3 | Nico

January 28th, 2009 at 3:34 am

Avatar

Hi Jennifer,

thank you, you were very helpfull.
One thing, in this part:

<?php edit_comment_link(__('(Edit)'),' ',")

the last double quote should be two single quotes.

By, Nico

4 | Jennifer

January 28th, 2009 at 7:56 am

Avatar

@Nico
Actually they were – but wordpress changed it to one double quote. /sigh. Time to dive in and figure out how to turn that off!! I've updated the post changing them to use ascii instead: & # 1 4 6 ;

5 | Christine

January 29th, 2009 at 10:47 pm

Avatar

If you had a functions.php file already for your theme (to make the sidebar widgets work), do you just add this to that file?

6 | Jennifer

January 30th, 2009 at 8:29 am

Avatar

Yeah – if you already have a functions.php file in your theme directory – you can just add that in there. :)

7 | How to Remove “Says” From WordPress 2.7 Threaded Comments | (Anti) Social Development

February 12th, 2009 at 4:14 am

Avatar

[…] The method that I used was the callback function, which is mentioned in the codex without much explanation and also detailed at ScriptyGoddess. […]

8 | Philip Arthur Moore

February 20th, 2009 at 1:58 am

Avatar

Thanks a ton for taking the time to outline wp_list_comments and also throw in your two cents. The Codex documentation on the function is terrible. And the idea that we "should" or "should not" do something as theme developers is insane. I love WordPress but this template tag has to be the worst tag I have ever encountered.

9 | zakkap

February 28th, 2009 at 2:44 pm

Avatar

hey great post – the wordpress codex has so little information about this. you explained it clearly. and who is wordpress to tell us that we "shouldn't need to change" the HTML.

10 | Upgrading Your Comments Area in Wordpress 2.7 | Addicott Web

March 6th, 2009 at 1:35 pm

Avatar

[…] Code for functions.php (from Scriptygoddess) […]

11 | Mark Jaquith

May 1st, 2009 at 1:05 am

Avatar

The reason for no ending </li> is for threaded comments, where the child comments have to be within the parent list item. If you close it here, WordPress won't be able to create a valid ol/li tree.

12 | Jeff

May 5th, 2009 at 9:51 pm

Avatar

The new comment features in 2.7 make a lot of things easier but you're so right in that it's now a freakin' pain to get rid of the "says"…

13 | Gerrit Fries

November 28th, 2009 at 5:55 pm

Avatar

Thanks a lot! I am building my own blog right now – so this was nice tip to use my own gravatar function (which stores image on my server) on comments.

14 | Scott H

January 4th, 2010 at 3:17 pm

Avatar

Thanks for saving the day again.

I'm working on a new WP install after not having done one in a while and I'm finding WP's hide the html code from the kids attitude pretty annoying.

15 | Scott H

January 4th, 2010 at 3:45 pm

Avatar

The code block you provided for functions.php is missing the php start tag at the beginning and a php end tag at the end of the code.

I'm guessing WP ate them up 😉

16 | Cenay : Wordpress Blogging Coach

January 25th, 2010 at 7:32 pm

Avatar

Interesting. I am working with a new theme that I believe BREAKS the silly comments. I am hoping this information will help me find the dang thing.

Implemented the new updates to the functions.php file first, along with the new call in my themes comments.php file. The interesting part is that bypasses the HTML to standard HTML didn't fix it.

Guess I am going to have to break down and buy the PHP debugger I have been eyeballing and trace where it goes wrong.

Thanks for your insights and help.

17 | KDesign

March 10th, 2010 at 9:05 pm

Avatar

Hi, thanks for the tutorial. I'm having problems though that I can't seem to find an answer to on the web.

When I follow the example in the Codex http://codex.wordpress.org/Template_Tags/wp_list_comments#Comments_Only_With_A_Custom_Comment_Display, as you've done in your article, I keep getting an error message after updating functions.php.

It says: Parse error: syntax error, unexpected '}' in /home4/popsicl2/public_html/wp-content/themes/magadine12/functions.php on line 26

Have you ever had this problem? I'm not sure where the problem would be seeing as I copied and pasted everything exactly, and I don't know if there are other files I should be looking at besides comments.php and functions.php. My functions.php file doesn't even have any other code in there besides this custom callback one.

Any help would be much appreciated! And if it helps, I'm using version 2.9.2. Thanks.

18 | Jennifer

March 10th, 2010 at 9:44 pm

Avatar

So I was testing this out to see if maybe something changed in the latest version – I had a test wordpress setup – and just dumped the mycomment function in the functions.php file and got the same error you did. But what I realized is that I had actually placed it BELOW the close php tag just above it… ie. the function looked like this before I put the new code in:
…last function in functions.php actually is printing something to screen…
<?php } ?>

And right here is where I dumped my function…

do you see the problem? (the php file is close just above where I put my function… so in that last line, I had to remove the close php:
<?php }
NEW FUNCTION GOES HERE..

That may not have been the same problem you're having – but you never know. It's just funny that it gave the same response you describe. Otherwise, once i fixed that, everything worked fine…

19 | Stephen Cronin

May 29th, 2010 at 6:20 pm

Avatar

Hi,

I just edited the documentation at http://codex.wordpress.org/Template_Tags/wp_list_comments#Parameters to change the "Not recommended" to "Use with caution", which I think is more appropriate. You know any one can do this right? :)

We'll see if they let it stand. Note that the new default theme in WordPress 3.0 (Twenty Ten) uses the callback function (to allow child themes to override it with their own code).

20 | Elliot

January 25th, 2011 at 4:55 am

Avatar

Not sure if this is just me, as im not too amazing with this but at the end you have:

<?php
}

Im assuming that <?php needs to be deleted as it will obviosuly cause a syntax error. Or am I just being stupid 😀

21 | Jennifer

January 25th, 2011 at 1:55 pm

Avatar

No – don't delete that part. The assumption is that you are placing this in the middle of a functions.php file – which already has a starting <?php in the beginning and and ending ?> – in the middle of that function we're basically "echoing" some HTML (not by explicitly saying echo "this"; but by closing the php tag – putting in the html and then opening up the php again to end the function… Not sure if that makes sense or not … either way… don't delete it :)

22 | Adding Comment Counts to Theme Files :: The English Guy Web Design

November 13th, 2011 at 7:17 am

Avatar

[…] Scriptgoddess has a good write-up of how to use wp_list_comments() with callback; […]

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements