WordPress wp_list_comments callback explanation

When working with comments you might want to drastically change the appearance of the comments. You can use a new comment template with

comments_template( '/my-comments.php', true )

which you call from the template where you want to list the comments. But if you want each single comment to be different, you can use the callback function of wp_list_comments.

You use wp_list_comments in your comments template. Default is comments.php in your theme function, but if you used the comments_template function from above, you have to look for that file (of course). You should see wp_list_comments like the The Loop on it’s own. It goes through all of the comments and lists them. With the callback you can modify The Comment Loop and totally make it to your own likings.


<?php 
global $my_object;
wp_list_comments(array('type' => 'all', 'callback', array($my_object, 'my_custom_comments'));
?>

I’m using classes in my plugins, hence the inclusion of the global, you can also put the function in your functions.php and simply remove the references to the object. Then you can create a function in your class or functions.php and start the fun:


function my_custom_comments($comment, $args, $depth){
        
        $GLOBALS['comment'] = $comment;
    
        comment_text();
        
    }

In it, you can use the Comments Tags which are listed here. Questions? I’m happy to help you a little further, so don’t hesitate to post!

This entry was posted in Uncategorized. Bookmark the permalink.