Я новичок в 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?
заранее спасибо