【问题标题】:Wordpress comments in the wrong order and gravatar not workingWordpress 评论顺序错误且 gravatar 无法正常工作
【发布时间】:2018-10-09 23:03:42
【问题描述】:

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

我创建了一个 cmets 类:

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

第一期: 结果是第一篇文章的 cmets 出现在最后一篇文章中。

第二期: 头像显示在文本“作者:”旁边

目前我只有一条评论,与“A WordPress Commenter”发表的第一篇文章有​​关

如果我使用comment_author(),那么会显示“匿名”——该用户不应该还有匿名类型的头像要显示吗?

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

Missing argument 1 for get_avatar(),

如何让作者的 id 传递给 get_avatar?

提前致谢

【问题讨论】:

  • 我能够通过使用以下命令找出 get_avatar 是 - get_avatar(get_comment_author_email(), $size = '16')

标签: php wordpress gravatar


【解决方案1】:

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

这里是 cmets 类:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多