admin管理员组文章数量:1201181
I'm editing my comments.php, and within it I've added a reply button like this:
<div class="reply">
<a rel="nofollow" class="comment-reply-link"
href=<?php comment_reply_link(array('reply_text' => 'Reply this comment'), comment_ID()); ?> data-commentid="3"
data-postid="487" data-belowelement="div-comment-3" data-respondelement="respond"
data-replyto="Reply to admin" aria-label="Reply to admin"> Reply
</a>
</div>
When I click on the "reply" button under any comment, the comment form pops up beneath the first comment, and only replies to the first one, rather than the one I clicked on. Can anyone explain the errors in this code and describe how to fix it?
I also tried doing
<div class="reply">
<?php comment_reply_link(); ?>
</div>
But the comment_reply_link()
returns null
, I believe.
I list the comments like so:
<?php
if (have_comments()) : ?>
<ol class="post-comments">
<?php
wp_list_comments(array(
'style' => 'ol',
'short_ping' => true,
'callback' => 'better_comments'
));
?>
</ol>
<?php
endif;
and have a function better_comments() {...}
which includes the above HTML for the reply div.
Here's the first bit of that function:
<li class="comment byuser comment-author-admin bypostauthor even thread-even depth-1" id="comment-3" style="margin-bottom: 0.7em;">
<div id="div-comment-3" class="comment-body">
<div class="comment-author vcard">
<a class="comment-author-text">
<b><?php echo ucwords(get_comment_author()) ?></b>
</a>
Thanks.
I'm editing my comments.php, and within it I've added a reply button like this:
<div class="reply">
<a rel="nofollow" class="comment-reply-link"
href=<?php comment_reply_link(array('reply_text' => 'Reply this comment'), comment_ID()); ?> data-commentid="3"
data-postid="487" data-belowelement="div-comment-3" data-respondelement="respond"
data-replyto="Reply to admin" aria-label="Reply to admin"> Reply
</a>
</div>
When I click on the "reply" button under any comment, the comment form pops up beneath the first comment, and only replies to the first one, rather than the one I clicked on. Can anyone explain the errors in this code and describe how to fix it?
I also tried doing
<div class="reply">
<?php comment_reply_link(); ?>
</div>
But the comment_reply_link()
returns null
, I believe.
I list the comments like so:
<?php
if (have_comments()) : ?>
<ol class="post-comments">
<?php
wp_list_comments(array(
'style' => 'ol',
'short_ping' => true,
'callback' => 'better_comments'
));
?>
</ol>
<?php
endif;
and have a function better_comments() {...}
which includes the above HTML for the reply div.
Here's the first bit of that function:
<li class="comment byuser comment-author-admin bypostauthor even thread-even depth-1" id="comment-3" style="margin-bottom: 0.7em;">
<div id="div-comment-3" class="comment-body">
<div class="comment-author vcard">
<a class="comment-author-text">
<b><?php echo ucwords(get_comment_author()) ?></b>
</a>
Thanks.
Share Improve this question edited May 27, 2022 at 17:12 Nicolas Gimelli asked May 27, 2022 at 16:33 Nicolas GimelliNicolas Gimelli 1034 bronze badges 10 | Show 5 more comments1 Answer
Reset to default 2Your comment replies do not work because you're using a custom comment rendering callback function named better_comments
, and this function hardcodes the classes IDs and other attributes rather than calling functions such as comment_ID
.
I strongly suggest abandoning this function, and instead opting for the default commenting from a default theme. This way you can start from a known good solution, and add the functionality you want correctly and incrementally.
Additionally, lots of the things you want to add do not need a custom comment renderer, e.g. changing the reply link text can be done with filters, as can adding extra links.
You might also have forgotten to enqueue the comment reply script:
// Threaded comment reply styles.
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
本文标签: Clicking Comment quotReplyquot Button only replies to first comment
版权声明:本文标题:Clicking Comment "Reply" Button only replies to first comment 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738564232a2099828.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
data-commentid="3" data-postid="487" data-belowelement="div-comment-3"
, and that your use ofcomment_reply_link
does not match the examples in the official documentation at developer.wordpress.org/reference/functions/comment_reply_link,comment_reply_link
does not output a URL, it outputs HTML tags – Tom J Nowell ♦ Commented May 27, 2022 at 16:58wp_list_comments
, e.g. developer.wordpress.org/themes/template-files-section/… – Tom J Nowell ♦ Commented May 27, 2022 at 17:04comment_reply_link
is supposed to returnnull
as it does not return anything, itecho
's instead and is a wrapper around another function – Tom J Nowell ♦ Commented May 27, 2022 at 17:05comment_ID()
andthe_ID()
displays/echo the output (comment/post ID). Useget_comment_ID()
andget_the_ID()
instead to get the comment/post ID, e.g. to be passed as a parameter to a function. – Sally CJ Commented May 27, 2022 at 17:09