我使用Bootstrap carousel在WordPress中显示特定帖子的3条评论,最新的评论必须在div上有active
类。然而,我的代码将active
类添加到所有3条评论中。因此,所有3个节目都显示在一个单独的转盘块中,其中它应该被3分割或分页。这是我当前的代码:
<?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 class="carousel-item active">
<div class="carousel-caption">
<span class="fad fa-quote-right" 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;
?>
我想要一个条件,其中if comment is latest, add $active = ' active'; else, show nothing
到<div class="carousel-item'.$active.'">
。
这可能吗?
给你。。。使用for而不是foreach,并检查这是否是第一个项目($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 class="carousel-item ' . ($i==0 ? "active" : "") . '">
<div class="carousel-caption">
<span class="fad fa-quote-right" 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;
?>