Add Ordinal Suffix to WordPress Counter

2019-09-06 01:56发布

问题:

I am trying to add ordinal suffixes to a WordPress Post Counter that I am building for a ranking board. Here is the code I currently have.

<?php if(have_posts()): $counter = 1; query_posts('post_type=rushmoor&meta_key=subaru_driver_best_lap&orderby=meta_value_num&order=asc'); while(have_posts()):the_post();?>
<?php $driver_best_lap = get_post_meta( get_the_ID(), 'subaru_driver_best_lap', true );?>
<li>
    <div class="name"><?php echo $counter;?> <?php the_title();?></div>
    <div class="lap-time"><?php echo $driver_best_lap;?></div>
</li>         
<?php $counter++; endwhile; wp_reset_query(); endif;?>

I have been trying to merge code from the following link with the above, however I cannot yield anything that actually works and am wondering if anyone can assist.

add 'rd or 'th or 'st dependant on number

Thanks Zach

回答1:

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>