我正在 Wordpress 中开发一个新闻网站,我需要在引导轮播中显示前三个帖子,我的问题是我只需要在三个元素中的第一个添加“活动”类,但真的不不知道怎么做。这是我的代码:
<?php
$args = array('numberposts' => '3');
$recent_posts = wp_get_recent_posts($args);
foreach ($recent_posts as $recent) {
echo '<div class="item active"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ': <strong>' .$recent["post_title"] . '</strong></a></div>';
}
?>
Run Code Online (Sandbox Code Playgroud)
我已经尝试过在这个网站上找到的答案(这个):
$isFirst = true;
foreach ($recent_posts as $recent) {
echo '<div class="item' . $isFirst ? ' active' : '' . '"><a href="' . get_permalink($recent["ID"]) . '" title=" ' . esc_attr($recent["post_title"]) . '" >' .$recent["post_date"] . ": <strong>" .$recent["post_title"] …Run Code Online (Sandbox Code Playgroud)