简体   繁体   English

“回复”链接未在 WordPress 自定义评论模板中附加评论作为回复

[英]'Reply' link is not attaching comment as a reply in WordPress custom comments template

This is my first foray into customising the default comments template in WordPress. I have looked at tutorials on how to achieve a custom comments layout and came across one that suited my needs.这是我第一次尝试自定义 WordPress 中的默认评论模板。我查看了有关如何实现自定义评论布局的教程,并找到了适合我需要的教程。 The design I am building requires a custom layout for the comments so I needed to create a new file.我正在构建的设计需要为评论自定义布局,因此我需要创建一个新文件。

It appears to post the comment correctly, but if I choose to reply to a comment, it does not attach it as a reply.它似乎正确地发布了评论,但如果我选择回复评论,它不会将其作为回复附加。 It adds it to the bottom of the comments stack.它将它添加到评论堆栈的底部。

I'm unclear how to resolve this and any additional guidance would be appreciated.我不清楚如何解决这个问题,如有任何额外指导,我们将不胜感激。

My custom template is as follows -我的自定义模板如下 -

// single-comments.php
<?php 

function boilerplate_comments($comment, $args, $depth) {

    // Get correct tag used for the comments
    if ( 'div' === $args['style'] ) {
        $tag       = 'div';
        $add_below = 'comment';
    } else {
        $tag       = 'li';
        $add_below = 'div-comment';
    } ?>

    <<?php echo $tag; ?> <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?> id="comment-<?php comment_ID() ?>">

    <?php
    // Switch between different comment types
    switch ( $comment->comment_type ) :
        case 'pingback' :
        case 'trackback' : ?>
        <div class="pingback-entry"><span class="pingback-heading"><?php esc_html_e( 'Pingback:', 'textdomain' ); ?></span> <?php comment_author_link(); ?></div>
    <?php
        break;
        default :

        

        if ( 'div' != $args['style'] ) { ?>
            <div id="div-comment-<?php comment_ID() ?>" class="comments__item">
        <?php } ?>
            <div class="comments__item--media">
                <?php echo get_avatar($comment, $size='100'); ?>
            </div><!-- .comments__item--media -->
            <div class="comments__item--content">

                <div class="comments__item--title">
                    <!-- Display author name -->
                    <h3><?php echo $comment->comment_author ?></h3>
                    <div class="comments__item--date">
                        Posted at <?php echo get_comment_time() ?>, <?php echo get_comment_date('d F') ?>
                    </div>
                    
                </div><!-- .comments__item--title -->

                <?php comment_text(); ?>

                <?php
                // Display comment moderation text
                if ( $comment->comment_approved == '0' ) { ?>
                    <em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'textdomain' ); ?></em><br/><?php
                } ?>

                <div class="comments__item--reply">
                <?php
                // Display comment reply link
                comment_reply_link( array_merge( $args, array(
                    'add_below' => $add_below,
                    'depth'     => $depth,
                    'max_depth' => $args['max_depth']
                ) ) ); 
                ?>
                </div>
                <div class="comments__item--edit">
                <?php edit_comment_link( __( 'Edit', THEME_NAME ), '  ', '' ); ?>
                </div>

            </div><!-- .comments__item--content -->
    <?php
        if ( 'div' != $args['style'] ) { ?>
            </div>
        <?php }
    // IMPORTANT: Note that we do NOT close the opening tag, WordPress does this for us
        break;
    endswitch; // End comment_type check.
}

In single.php I call it by -在 single.php 我称之为 -

// single.php

<?php comments_template('templates/single-comments.php'); ?>

in functions.php I require the template by -在 functions.php 我需要模板 -


require_once(DIR . '/templates/single-comments.php');

And set the callback to -并将回调设置为 -

add_filter( 'wp_list_comments_args', function( $args ) {
    $args[ 'callback' ] = 'boilerplate_comments';
    return $args;
} );

You do not need to require the template file, this is done automatically in the comments_template function. pass the comment object as the second parameter to comment_reply_link():您不需要模板文件,这是在 comments_template function 中自动完成的。将评论 object 作为第二个参数传递给 comment_reply_link():

comment_reply_link( array_merge( $args, [
            'depth'      => $depth,
            'add_below'  => 'comment-reply-target',
            'max_depth'  => $args['max_depth'],
            'reply_text' => __( 'Reply to this comment ...', 'stack' ),
        ] ), $comment )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM