Add Ordinal Suffix to WordPress Counter

我的未来我决定 提交于 2019-12-11 17:54:14

问题


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>


来源:https://stackoverflow.com/questions/23274562/add-ordinal-suffix-to-wordpress-counter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!