WordPress评论回复区手动添加点赞按钮:代码插入求助
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:
- Open your theme's
comments.phpfile (or a comment template part liketemplate-parts/comment.phpif your theme uses modular files). - Look for the section where individual comments are displayed. This might be inside a
foreachloop over$commentsor usingwp_list_comments()with a custom callback function. - 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:
- In
comments.php, find where thecomment_form()function is called. - 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 liketemplate-parts/comment.phporcomments/comment-item.php(the exact name varies by theme).
内容的提问来源于stack exchange,提问作者Adham Mohamed

