I am new to WordPress and I am trying to apply some of my OOP knowledge, which has lead me to be stuck and seeking your help
I created a comments class:
<?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();
The following is a portion of my index.php page:
<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.
Second issue: The gravatar is showing up next to the text "Author: "
I only have one comment so far, which is related to the first post, made by "A WordPress Commenter"
If I use comment_author(), then is shows "Anonymous" - shouldn't that user still have an anonymous type of gravatar to be displayed?
If I try get_avatar() instead of wp_list_comments( array( 'avatar_size' => '16' ), then I receive the following error:
Missing argument 1 for get_avatar(),
How do I get the author's id to pass to get_avatar?
Thanks in advance
Figured it out. You have to load the object first, then called the getComment function from within the while loop. I will select this as the complete answer in a couple of days, when stackoverflow system will allow it
Here is the comments class:
<?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();
The following is a portion of the index page:
<?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>