You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress评论回复区手动添加点赞按钮:代码插入求助

Adding the Simple Likes Button to Your Comment Section

Hey there! Let's work through getting that like button code into your comment section. The trick is targeting the right spot in your comments.php file, so let's break this down based on what you're trying to achieve:

Option 1: Add the Like Button to Each Individual Comment

This is the most common scenario—letting users like specific comments. You'll need to find the loop that renders each comment in your template.

Here's how:

  1. Open your theme's comments.php file (or a comment template part like template-parts/comment.php if your theme uses modular files).
  2. Look for the section where individual comments are displayed. This might be inside a foreach loop over $comments or using wp_list_comments() with a custom callback function.
  3. Insert your button code where you want it to appear—like next to the reply link, below the comment content, or near the comment metadata (author name/date). For example:
    <?php echo get_simple_likes_button( get_comment_ID(), 1 ); ?>
    

If your comment loop looks something like this:

<?php foreach ( $comments as $comment ) : ?>
  <article class="comment">
    <div class="comment-meta">
      <span class="comment-author"><?php comment_author(); ?></span>
      <span class="comment-date"><?php comment_date(); ?></span>
    </div>
    <div class="comment-content"><?php comment_text(); ?></div>
    <!-- Place the like button here -->
    <div class="comment-like"><?php echo get_simple_likes_button( get_comment_ID(), 1 ); ?></div>
    <?php comment_reply_link(); ?>
  </article>
<?php endforeach; ?>

Drop the button code in the spot that makes the most sense for your layout.

Option 2: Add the Like Button Inside the Reply Form (For the Parent Comment)

If you want to show the like button for the comment someone is replying to, you can add it right above or inside the reply form.

Try this:

  1. In comments.php, find where the comment_form() function is called.
  2. Add this code right before or after that function to display the like button only when the user is replying to a comment:
    <?php if ( isset( $_GET['replytocom'] ) ) : ?>
      <?php $parent_comment = get_comment( absint( $_GET['replytocom'] ) ); ?>
      <div class="reply-comment-like">
        <?php echo get_simple_likes_button( $parent_comment->comment_ID, 1 ); ?>
      </div>
    <?php endif; ?>
    <?php comment_form(); ?>
    

This pulls the parent comment ID from the replytocom query parameter and renders its like button when the reply form is active.

Quick Troubleshooting Tips

  • Function not working? Double-check that get_simple_likes_button() is properly loaded (from the Simple Likes plugin or your custom code—if it's a custom function, make sure it's defined somewhere in your theme or plugin files).
  • Wrong comment ID? For Option 1, ensure you're calling get_comment_ID() inside the comment loop—outside the loop, it won't return the correct comment ID.
  • Theme uses template parts? If you don't see the comment loop in comments.php, check smaller template files like template-parts/comment.php or comments/comment-item.php (the exact name varies by theme).

内容的提问来源于stack exchange,提问作者Adham Mohamed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:43:50