There are several walker classes in WordPress – I don’t actually know how many there are because I tend to browse the source code only if I need to change something, just as others I guess :). Regardless, I’m sure that custom navigation walker class is probably the most used and customized WordPress walker class. But recently I had a project where I needed to change meta info which appears next to Gravatar image in the comments section. When it comes to any walker classes in WordPress, I guess that the main idea is that you take the whole code, put it into functions.php or elsewhere and use function callback to call it. I guess…
So first things first – the function which calls comments is wp_list_comments() which also accepts number of parameters. One of them is ‘walker’. In order to call walker, it would be nice to create one. You can call it like this:
1 2 3 4 5 6 |
wp_list_comments( array( 'style' => 'ol', 'short_ping' => true, 'avatar_size' => 82, 'walker' => new bruno_walker_comment ) ); |
But as I said, we need a walker:
That’s a lot of code, right? But don’t worry, About 99% of that code is copy-paste from class-walker-comment.php file, which lives in wp-includes folder. The only change I did was this:
1 2 3 4 5 6 7 8 9 |
<div class="comment-metadata"> <?php $user_id=$comment->user_id; ?> <p class="commenter-bio"><?php the_author_meta('description',$user_id); ?></p> <p class="commenter-url"><a href="<?php the_author_meta('user_url',$user_id); ?>" target="_blank"><?php the_author_meta('user_url',$user_id); ?></a></p> </div><!-- .comment-metadata --> |
I used this code to get some data ( biographical info and user URL) from Users section. So to conclude, if you want to make some bigger changes in the comments section, walker class is the way to go. All you need to do is copy the original walker and extend it with your own changes.