wordpress为文章和页面添加评论功能

新建文件comments.php文件

<?php
     if (isset($_SERVER['SCRIPT_FILENAME']) && 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']))
        die ('Please do not load this page directly. Thanks!');
if ( post_password_required() ) {
    return;
}
?>
<div id="comments" class="comments-area">
    <?php if ( have_comments() ) : ?>
        <h3 class="comments-title">
            <?php printf(get_the_title());?>的
            <?php printf(get_comments_number());?>个评论:
        </h3>
        <ol class="comment-list">
            <?php
                wp_list_comments( array(
                    'style'       => 'ol',
                    'short_ping'  => true,
                    'avatar_size' => 56,
                ) );
            ?>
        </ol><!-- .comment-list -->
    <?php endif; // have_comments() ?>
    <?php
        if ( ! comments_open() && get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
    ?>
        <p class="no-comments"><?php _e( 'Comments are closed.', 'twentyfifteen' ); ?></p>
    <?php endif; ?>
    <?php comment_form(); ?>
</div>

页面引入

<?php comments_template(); ?>

自定义样式

add_filter('comment_form_default_fields', 'mytheme_remove_commentform_fields');
function mytheme_remove_commentform_fields($fields){
    $fields =  array(
       'author' => '<div class="author"><input id="author" name="author" type="text" placeholder="昵称(必填)" value="' .
            esc_attr($commenter['comment_author']) . 
            '" size="30" />',
        'email' => '<input type="text" placeholder="邮箱(必填,不会公开)" aria-required="true" size="30" value="' . 
            esc_attr($commenter['comment_author_email']) . 
            '" name="email" id="email"></div>',
        'url' => '',
    );
    return $fields;
}

页面评论

wordpress页面评论后续版本默认是关闭的需要手动打开,进入页面编辑界面,选择右上角显示选项,勾选讨论评论,在页面内容下方讨论中勾选允许评论即可