WordPress的评论顺序错误,Gravatar无法正常工作

时间:2018-10-09 23:03:43

标签: php wordpress gravatar

我是WordPress的新手,我正在尝试应用一些OOP知识,这使我陷入困境并寻求您的帮助

我创建了一个注释类:

    <?php

class Comments {
  public $commentSection;

  public function getComments() {
    //Get only the approved comments 
    $args = array(
      'status' => 'approve'
    );

    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );
    if ( $comments ) {
      $this->commentSection = "
      <article class='post'>
        <header>
          <h3> Comments</h3>
        </header>
        <p>
      ";
      foreach ( $comments as $comment ) {
        $this->commentSection .= 'Author: ' . wp_list_comments( array( 'avatar_size' => '16' ) );
        $this->commentSection .= 'Date: ' . comment_date();
        $this->commentSection .= 'Comment: ' . $comment->comment_content;
      }
    $this->commentSection .= "
        </p>
      </article>
    ";
    } else {
      $this->commentSection = '';
    }
    echo $this->commentSection;
  }
}

$commentsObj = new Comments();
$commentsObj->getComments();

以下是我的index.php页面的一部分:

<section>
<div class="container">
  <?php
    if(have_posts()){
      while(have_posts()){
        the_post();
      ?>
        <article class="post">
          <header>
            <a href=" <?php the_permalink(); ?> " target='_self'><h1> <?php the_title(); ?> </h1></a>
          </header>
          <p> 
            <?php the_content(); ?> 
          </p>
        </article>
        <?php
          require_once('includes/comments.inc.php');
        ?>
      <?php
      }
    }
  ?>
</div>

第一个问题: 结果是第一篇文章的评论显示在最后一篇文章中。

第二个问题: 该图形显示在文本“作者:”旁边

到目前为止,我只有一个评论,与“ A WordPress Commenter”发表的第一篇帖子有关。

如果我使用comment_author(),则显示为“ Anonymous”(匿名)-该用户是否还应该具有要显示的匿名Gravatar类型?

如果我尝试使用get_avatar()而不是wp_list_comments(array('avatar_size'=>'16'),则会收到以下错误消息:

Missing argument 1 for get_avatar(),

如何获取作者的ID传递给get_avatar?

预先感谢

1 个答案:

答案 0 :(得分:0)

弄清楚了。您必须先加载对象,然后在while循环中调用getComment函数。几天后,当Stackoverflow系统允许的时候,我会选择这个作为完整答案

这是评论课:

    <?php

class Comments {
  public $commentSection;
  public $commentPostId;
  public $commentArr;

  public function getComments() {
    //Get only the approved comments 
    $args = array(
      'status' => 'approve'
    );

    $comments_query = new WP_Comment_Query;
    $comments = $comments_query->query( $args );
    if ( $comments ) {
      foreach ( $comments as $comment ) {
        $this->commentArr = get_comment( get_the_author_meta('ID') );
        $this->commentPostId = $this->commentArr->comment_post_ID;
        // echo " comment post id: " . $this->commentPostId;
        // echo " post id: " . get_the_ID();
        if($this->commentPostId == get_the_ID()){
          $this->commentSection = "
          <article class='post'>
            <header>
              <h3> Comments</h3>
            </header>
            <p>
          ";
          $this->commentSection .= 'Author: ' . get_avatar(get_comment_author_email(), $size = '16') . " comment id: " . $this->commentPostId;
          $this->commentSection .= 'Date: ' . comment_date();
          $this->commentSection .= 'Comment: ' . $comment->comment_content;
          $this->commentSection .= "
            </p>
          </article>
          ";
        }
      }
    echo $this->commentSection;
    } else {
      $this->commentSection = '';
    }
  }
}

$commentsObj = new Comments();

以下是索引页面的一部分:

      <?php
    require_once('includes/comments.inc.php');
  ?>
  <?php
    if(have_posts()){
      while(have_posts()){
        the_post();
      ?>
        <article class="post">
          <header>
            <a href=" <?php the_permalink(); ?> " target='_self'><h1> <?php the_title(); ?> </h1></a>
          </header>
          <p> 
            <?php the_content(); ?> 
          </p>
        </article>
        <?php
         // had to add this for home page
         if( get_comment( get_the_author_meta('ID') )->comment_post_ID == get_the_ID() ) {
            $commentsObj->getComments();
           }
        ?>
      <?php
      }
    }
  ?>
</div>