'回复'链接未在 WordPress 自定义评论模板中附加评论作为回复
这是我第一次尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要需要模板文件,这是在 comments_template 函数中自动完成的。
将评论对象作为第二个参数传递给 comment_reply_link() :
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() :