2

我在 wordpress 中设置了一个 comment() 条件。就像 wordpress 默认主题一样,这个条件在 comments.php 中设置。

然后使用comment_template加载整个comments.php文件;现在,当我删除 have_comments() 条件时,一切正常并且所有评论都已加载,但是当我添加此条件时,它返回 false,就好像没有评论一样。

这是我的整个 comments.php 文件:

<?php
/**
| This page deals with the comment-system and template in the Behdis Marketing Group Wordpress Theme.   
**/
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$fields =  array(
    'author' => "<div><input type='text' name='author' placeholder='Full Name' /></div>",
    'email'  => "<div><input type='text' name='email' placeholder='Email /></div>", 
);

$comments_args = array(
    'fields' =>  $fields,
    'comment_field' => "<div class=\"comment-component\"><textarea name=\"comment\" id=\"comment\" ></textarea></div>",
    'comment_notes_after' => '',
    'title_reply' => 'Write your comment...',
    'title_reply_to' => 'Reply',
    'label_submit' => 'Comment!',
    'comment_notes_before' => "<p class='simple-title'>" . 'Your email is kept secret forever' . ' '
);

comment_form($comments_args);
?>
<?php

    if( have_comments() )
    {       
?>
<section class='post-comments'>

    <?php
        $comments = get_comments();
        foreach($comments as $comm)
        {
            ?>
            <div class='post-each-comment'>
            <p class="post-each-comment-meta">
            <?php echo $comm->comment_author;?> در تاریخ <?php comment_time();?>
            </p>
            <?php echo $comm->comment_content;   ?>
            </div>
            <?php
        }
        ?>
</section>
    <?php
    }// end of have_comments()
   else
    {
        ?>
        <div class='no-comment' >
            No comments, be the first visitor to comment on this post!
        </div>
        <?php   
    }
    ?>

提前致谢

4

3 回答 3

7

你打电话have_comments()之前打电话get_comments()

这可能只是您在这里处理错误的问题。Wordpress 利用全局静态状态,所以事情的顺序很重要(而且很容易错过):

<?php

    $comments = get_comments();

    if( have_comments() )
    {       
?>
<section class='post-comments'>

    <?php        
        foreach($comments as $comm)
        {
            ?>
            <div class='post-each-comment'>

法典还说,这have_comments()取决于循环,所以就是$post. 可能是即使我上面的示例代码建议也无法正确处理静态状态,因此您需要对其进行故障排除以找出要使用的内容。

例如 asget_comments()返回一个数组,这通常是这样做的:

<?php

    $comments = get_comments();

    if( $comments )
    {       
?>
<section class='post-comments'>

    <?php        
        foreach($comments as $comm)
        {
            ?>
            <div class='post-each-comment'>

如您所见,have_comments()不需要调用。

希望这会有所帮助并保重。

于 2013-08-20T08:04:36.620 回答
3

当您在设置评论之前查询评论时会出现此问题。您可以使用comments_template(..)加载包含have_comments()正确设置评论的页面。

于 2017-01-30T09:45:07.463 回答
0

我为此花了几个小时。可能对某人有帮助。

当我尝试get_template_part('comments');在我的模板中使用以在页面上使用 comments.php 时 - 它不起作用并have_comments();返回给我bool(false)

感谢这篇文章。我使用comments_template();了 get_template_part。它有效。我认为主要原因是 Wordpress 使用了全局静态,您需要使用 comments_template() 和单个帖子或页面来保存它。

于 2021-11-25T15:41:29.730 回答