WordPress 中的评论为空

Comments are empty in WordPress

尽管我已将 <?php comments_template(); ?> 添加到 single.php,但它似乎不起作用!我确定了,但是为什么要批准的评论列表中的评论是空的?每条评论的姓名、电子邮件和 URL 字段以及内容均为空。

wp-comments-post.php 应该与其余主题文件放在一起还是应该与主文件放在一起?

这个问题的原因可能是什么?

函数 comments_template() 在主题目录的根目录中查找名为 comments.php 的文件。 comments.php 中的代码应如下所示:

<!-- Display the comments -->
<?php if($comments) : ?>
    <ol>
    <?php foreach($comments as $comment) : ?>
        <li id="comment-<?php comment_ID(); ?>">
            <?php if ($comment->comment_approved == '0') : ?>
                <p>Your comment is awaiting approval</p>
            <?php endif; ?>
            <?php comment_text(); ?>
            <cite><?php comment_type(); ?> by <?php comment_author_link(); ?> on <?php comment_date(); ?> at <?php comment_time(); ?></cite>
        </li>
    <?php endforeach; ?>
    </ol>
<?php else : ?>
    <p>No comments yet</p>
<?php endif; ?>

<!-- Display the form -->

<?php if(comments_open()) : ?>
<?php if(get_option('comment_registration') && !$user_ID) : ?>
    <p>You must be <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?redirect_to=<?php echo urlencode(get_permalink()); ?>">logged in</a> to post a comment.</p><?php else : ?>
    <form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
        <?php if($user_ID) : ?>
            <p>Logged in as <a href="<?php echo get_option('siteurl'); ?>/wp-admin/profile.php"><?php echo $user_identity; ?></a>. <a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=logout" title="Log out of this account">Log out &raquo;</a></p>
        <?php else : ?>
            <p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" />
            <label for="author"><small>Name <?php if($req) echo "(required)"; ?></small></label></p>
            <p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" />
            <label for="email"><small>Mail (will not be published) <?php if($req) echo "(required)"; ?></small></label></p>
            <p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" />
            <label for="url"><small>Website</small></label></p>
        <?php endif; ?>
    </form>
<?php endif; ?>
<?php else : ?>
    <p>The comments are closed.</p>
 <?php endif; ?>

但是,如果您有不同的主题目录布局,例如自定义 comments.php,您可以将其路径作为函数参数传入。例如,如果您的主题有一个模块目录:

<?php comments_template('modules/custom-comments.php'); ?>

结论

这可能是代码问题或误用 comments_template() 函数。我建议您不要更改任何 WordPress 代码,因为它会被未来的更新覆盖。

此答案基于:https://codex.wordpress.org/Function_Reference/comments_template

代码掉落取自:

http://code.tutsplus.com/articles/unraveling-the-secrets-of-wordpress-commentsphp-file--net-28