You want to add the ordinal_suffix
function from the other answer to your WordPress functions.php
file so you can use it in your theme. Then you want to pass your $counter
output to it. You'll want to change that line to:
<div class="name"><?php echo ordinal_suffix($counter);?> <?php the_title();?></div>