2

I'm trying to write a custom comment class for my comments and I've been trying to de-construct the code for quite a while, here it is:

class Custom_Comment_Walker extends Walker_Comment {
    var $tree_type = 'comment';
    var $db_fields = array( 'parent' => 'comment_parent', 'id' => 'comment_ID' );

    // constructor – wrapper for the comments list
    function __construct() { ?>
        <?php echo "Walker loaded."; ?>
        <section class="comments-list">

    <?php }

    // start_lvl – wrapper for child comments list
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        $GLOBALS['comment_depth'] = $depth + 1; ?>

        <section class="child-comments comments-list">

    <?php }

    // end_lvl – closing wrapper for child comments list
    function end_lvl( &$output, $depth = 0, $args = array() ) {
        $GLOBALS['comment_depth'] = $depth + 1; ?>

        </section>

    <?php }

    // start_el – HTML for comment template
    function start_el( &$output, $comment, $depth = 0, $args = array(), $id = 0 ) {
        $depth++;
        $GLOBALS['comment_depth'] = $depth;
        $GLOBALS['comment'] = $comment;
        $parent_class = ( empty( $args['has_children'] ) ? '' : 'parent' ); 

        if ( 'article' == $args['style'] ) {
            $tag = 'article';
            $add_below = 'comment';
        } else {
            $tag = 'article';
            $add_below = 'comment';
        } ?>

        <article <?php comment_class(empty( $args['has_children'] ) ? '' :'parent') ?> id="comment-<?php comment_ID() ?>" itemprop="comment" itemscope itemtype="http://schema.org/Comment">
            <figure class="gravatar"><?php echo get_avatar( $comment, 65, '[default gravatar URL]', 'Author’s gravatar' ); ?></figure>
            <div class="comment-meta post-meta" role="complementary">
                <h2 class="comment-author">
                    <a class="comment-author-link" href="<?php comment_author_url(); ?>" itemprop="author"><?php comment_author(); ?></a>
                </h2>
                <time class="comment-meta-item" datetime="<?php comment_date('Y-m-d') ?>T<?php comment_time('H:iP') ?>" itemprop="datePublished"><?php comment_date('jS F Y') ?>, <a href="#comment-<?php comment_ID() ?>" itemprop="url"><?php comment_time() ?></a></time>
                <?php edit_comment_link('<p class="comment-meta-item">Edit this comment</p>','',''); ?>
                <?php if ($comment->comment_approved == '0') : ?>
                <p class="comment-meta-item">Your comment is awaiting moderation.</p>
                <?php endif; ?>
            </div>
            <div class="comment-content post-content" itemprop="text">
                <?php comment_text() ?>
                <?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
            </div>

    <?php }

    // end_el – closing HTML for comment template
    function end_el(&$output, $comment, $depth = 0, $args = array() ) { ?>

        </article>

    <?php }

    // destructor – closing wrapper for the comments list
    function __destruct() { ?>

        </section>

    <?php }
}

As per the documentation from WordPress Developers:

The variable determines how much depth each level goes for, so, if I decide to add a depth of 1 to my initial comment, only 1 level of replies will be allowed.

Unfortunately, this also doesn't seem to be the case.

  • It's not enforcing limits, it's only for rendering. You can only set the maximum depth globally in WP's settings. – janh Jan 1 '18 at 9:03
  • @janh Janh, thank you for your input! Could you please provide some links or an answer with exactly what it does? MUCH appreciated! – Jonathan Guerin Jan 1 '18 at 14:21
1

The $depth parameter in the walker class is used for outputting and padding the output. You can use it to format your HTML structure. For example:

<div class="comment-meta post-meta cmnt-padding-<?php echo $depth ;?>" role="complementary">

which will output an extra class for your comment wrapper, that you can use to style your comments.

|improve this answer|||||
  • Jack. Thank you. Exactly what do you mean by the padding? Is it a class that I use for playing with CSS? To style different depths of comments differently? – Jonathan Guerin Jan 1 '18 at 16:06
  • You're welcome. Yes that's it. You can even use it to detect which level of comments you are outputting. Totally optional. – Jack Johansson Jan 1 '18 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.