How to Print/echo a css class only for 1st post and ignore all post after 1st?

I am trying to get a function to get three posts and print/echo 1st post with class active and rest of the post will be Normal.

I tried this code in functions.php to generate and didn’t achieve the result.

function slider_news() {
$args = array(
‘numberposts’ => 3
);
$latest_posts = get_posts($args);

foreach ($latest_posts as $post) {
$num = 0;
$newnum = $num + 1;
$num = $newnum;

if ($num =1) {

echo ‘Post with <div class=”active”>1st Post </div ‘;

$newnum = $num + 1;

}

else{

echo ‘Post with <div class=”Normal”>All other posts except 1st</div>’;

}
}
}

Source Code i got from this function is below

Post with <div class=”active”>1st Post </div>
Post with <div class=”active”>1st Post </div>
Post with <div class=”active”>1st Post </div>

Read more here:: How to Print/echo a css class only for 1st post and ignore all post after 1st?

Leave a Reply

Your email address will not be published. Required fields are marked *