如何仅向WordPress文章作者显示评论表单,同时对所有人展示评论?
WordPress评论表单权限控制异常排查
我需要实现以下逻辑:获取当前文章的作者ID,与登录用户ID对比,仅当两者匹配或用户是管理员时显示评论表单,否则隐藏。目前已确认ID获取和对比逻辑正常(通过页面底部输出的ID验证条件已满足),但评论表单仍处于隐藏状态。
不确定该将权限判断逻辑放在自定义评论模板review-comments.php,还是包含该模板的单篇页面模板single-review.php中,当前尝试是在review-comments.php中编写代码,代码如下:
<?php if (have_comments()) : ?> <div class="escortreviewtext"> <h4 class="rad3">Escort Reply:</h4> <div class="commentlistall"> <?php wp_list_comments(array('callback' => 'theme_comments', 'style' => 'div', 'type' => 'comment')); ?> <div class="clear"></div> </div> </div> <?php endif; ?> <div class="clear30"></div> <?php if (is_user_logged_in()) : $current_user = wp_get_current_user(); $post = get_post(); // 获取当前文章对象 $post_author_id = $post->post_author; if ($current_user->ID === $post_author_id || current_user_can('administrator')) : ?> <div class="commform" id="respond"> <form action="<?php echo esc_url(get_option('siteurl') . '/wp-comments-post.php'); ?>" method="post" id="commentform"> <div class="commtext col-100"> <?php _e('', 'escortwp'); ?> <div class="clear"></div> <textarea name="comment" id="comment" class="commtextarea" placeholder="Thanks dear❤️" tabindex="1" rows="10" style="width: 96%; border-radius: 5px; padding: 10px; margin-left: 10px; border: 1px solid #FE5ACB99;"></textarea> </div> <div class="clear10"></div> <div class="text-center"> <input type="submit" class="pinkbutton commsubmitbutton rad25" name="submit" value="<?php _e('Reply', 'escortwp'); ?>" tabindex="2" /> </div> <?php $replytoid = isset($_GET['replytocom']) ? (int)$_GET['replytocom'] : 0; ?> <input type='hidden' name='comment_parent' id='comment_parent' value='<?php echo $replytoid; ?>' /> <input type="hidden" name="comment_post_ID" value="<?php echo esc_attr($post->ID); ?>" /> <?php do_action('comment_form', $post->ID); ?> <a rel="nofollow" id="cancel-comment-reply-link" href="#respond" class="cancel-comment-reply-link rad5" style="display: none;"><?php _e('Cancel reply', 'escortwp'); ?></a> </form> </div> <!-- 评论表单容器 --> <?php endif; endif; ?> <!-- 页面底部输出ID用于验证 --> <footer> <p>当前用户ID: <?php echo $current_user->ID; ?></p> <p>文章作者ID: <?php echo $post_author_id; ?></p> </footer>
内容的提问来源于stack exchange,提问作者Godman Umoeka
相关产品推荐
相关产品推荐

