Комментарии Wordpress в неправильном порядке и граватар не работает

Я новичок в WordPress, и я пытаюсь применить некоторые свои знания ООП, что привело к тому, что я застрял и ищу вашу помощь.

Я создал класс комментариев:

    <?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>

First issue: The result is that comments for the first post are showing up on the last post.

Вторая проблема: рядом с текстом «Автор:» отображается граватар.

Пока у меня есть только один комментарий, связанный с первым постом, сделанным «комментатором WordPress».

Если я использую comment_author(), то отображается «Аноним» - разве у этого пользователя не должен отображаться анонимный тип граватара?

Если я попробую get_avatar() вместо wp_list_comments(array('avatar_size' => '16'), то я получу следующую ошибку:

Missing argument 1 for get_avatar(),

Как передать идентификатор автора в get_avatar?

заранее спасибо


person kronus    schedule 09.10.2018    source источник
comment
Я смог выяснить, что get_avatar использует следующее: get_avatar(get_comment_author_email(), $size = '16')   -  person kronus    schedule 10.10.2018


Ответы (1)


Догадаться. Вы должны сначала загрузить объект, а затем вызвать функцию getComment из цикла while. Я выберу это как полный ответ через пару дней, когда система 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>

person kronus    schedule 10.10.2018