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

发布于 2025-01-13 07:51:31 字数 3930 浏览 0 评论 0原文

这是我第一次尝试在 WordPress 中自定义默认评论模板。我查看了有关如何实现自定义注释布局的教程,并找到了适合我需求的布局。我正在构建的设计需要自定义注释布局,因此我需要创建一个新文件。

它似乎正确地发布了评论,但如果我选择回复评论,它不会将其附加为回复。它将其添加到评论堆栈的底部。

我不清楚如何解决这个问题,任何额外的指导将不胜感激。

我的自定义模板如下 -

// 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.
}

在 single.php 中,我通过以下方式调用它 -

// single.php

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

在functions.php 中,我通过 - 调用模板 -


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

并将回调设置为 -

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

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. 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

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

in functions.php I require the template by -


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;
} );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2025-01-20 07:51:31

您不需要需要模板文件,这是在 comments_template 函数中自动完成的。
将评论对象作为第二个参数传递给 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 )

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() :

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 )
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文