How to add extra class to last post WordPress

Hello friends, welcome to my another blog post how to add extra class to last post WordPress. Few days earlier I was woking with news magazine website in WordPress. Here I got the problem, I have to add an extra class into my last blog post. So I found the solution in google. But did not get proper solution for first 1 hour. Then I suddenly got some solutions from a different blog site. So I want to share the exact solutions with you. You can use this solution to add an extra class, add some div or any other work you want to do on your last post. So Below are the solutions:

Solution 1:

<?php if( ($wp_query->current_post + 1) < ($wp_query->post_count) ) {
 // Do something if it's the last post like $class='lastpost' then use this class into your blog div
};
?>

Solution 2:




<?php
$postCount = 0;

// Start of The Loop

if (++$postCount == 1) {
     // FIRST POST
} 

if ($postCount == sizeof($posts)) {
     // LAST POST IN LOOP
}

// End of The Loop
?>

Please share my blog if you think it is helpful for you. If you have any suggestions please comment below. Thank you

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •