I'm using Bootstrap carousel to display 3 comments of a specific post in WordPress and the latest comment must have the active
class on the div. However, the code that I have adds the active
class to all 3 comments. Hence, all 3 shows in a single carousel block, where it should be divided or paginated by 3. Here is my current code:
<?php
$args = array(
'status' => 'approve',
'number' => '1',
'post_id' => 9,
'orderby' => 'comment_date',
'order' => 'DESC'
);
$comments = get_comments( $args );
foreach ( $comments as $comment ) :
echo '<div >
<div >
<span aria-hidden="true"></span>';
comment_text();
echo '<div id="image-caption">— <a href="'.$comment->comment_author_url.'" target="_blank">'.$comment->comment_author.'</a></div>
</div><!-- carousel-caption -->
</div><!-- carousel-item -->';
endforeach;
?>
I would like to have a condition where if comment is latest, add $active = ' active'; else, show nothing
to the <div >
.
Is this possible?
CodePudding user response:
Here you go... Use for instead of foreach and check if that is the first item ($i==0)
<?php
$args = array(
'status' => 'approve',
'number' => '1',
'post_id' => 9,
'orderby' => 'comment_date',
'order' => 'DESC'
);
$comments = get_comments( $args );
for($i=0; $i<count($comments); $i ) :
$comment = $comments[$i];
echo '<div active" : "") . '">
<div >
<span aria-hidden="true"></span>';
comment_text();
echo '<div id="image-caption">— <a href="'.$comment->comment_author_url.'" target="_blank">'.$comment->comment_author.'</a></div>
</div><!-- carousel-caption -->
</div><!-- carousel-item -->';
endfor;
?>