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
  • 1 are you using the standard commenting APIs to list comments or have you implemented custom code to display comments? I notice you've hardcoded several parts of the HTML data attributes: data-commentid="3" data-postid="487" data-belowelement="div-comment-3", and that your use of comment_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:58
  • 1 can you share more of your code so that we can see? The code you used in your questions code block is definitely broken, what you just posted in the comment replying to me is a major improvement. For the comment replying functionality to work you need the appropriate HTML IDs in place, the reply JS script, and the right functions to be used, e.g. comments are meant to be displayed using wp_list_comments, e.g. developer.wordpress.org/themes/template-files-section/… – Tom J Nowell Commented May 27, 2022 at 17:04
  • 1 also comment_reply_link is supposed to return null as it does not return anything, it echo's instead and is a wrapper around another function – Tom J Nowell Commented May 27, 2022 at 17:05
  • 1 @NicolasGimelli, comment_ID() and the_ID() displays/echo the output (comment/post ID). Use get_comment_ID() and get_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
  • 1 Noting that the information needed to answer this question was added when the OP re-asked the question at wordpress.stackexchange.com/questions/406164/… which has now been closed as a duplicate. Do not post the same question multiple times. – Tom J Nowell Commented May 27, 2022 at 22:45
 |  Show 5 more comments

1 Answer 1

Reset to default 2

Your 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