我已经在wordpress中设置了comment()条件。就像wordpress默认主题一样,在comment.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
    }
    ?>

提前致谢

最佳答案

您先致电 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。甚至可能是我上面的示例代码建议也无法使其正确处理静态状态,因此您需要进行一些故障排除以找出要使用的内容。

例如。由于get_comments()返回一个数组,通常这样做:
<?php

    $comments = get_comments();

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

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

如您所见,无需调用have_comments()

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

07-24 09:47
查看更多