I have a WordPress function that allows me to displays "Todays" posts on a page and I am trying to take it further to split it into "Morning" and "Afternoon".

Here is the code I am using for Todays Posts:

function my_posts_where_from_today( $where = '' ) {
$where .= " AND post_date > '" . date('Y-m-d', strtotime('now')) . "'";
return $where;
}

This sits in functions.php and I call the filter within the post.

I am also using ordinal suffixes that are dynamically generated as shown in this question:

Add Ordinal Suffix to WordPress Counter

In order to split my posts into morning/afternoon, here is what I am doing:

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

Afternoon:

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

The problem is that my counter is getting messed up.

See the following links for examples:

http://www.subarurallyexperience.co.uk/rushmoor/ranking/today/ (shows all todays posts perfectly) http://www.subarurallyexperience.co.uk/rushmoor/ranking/today/morning/ (shows only morning posts, but counter is messed up) http://www.subarurallyexperience.co.uk/rushmoor/ranking/today/afternoon/ (shows only afternoon posts, but counter is messed up)

I have been trying for like an hour to fix the counter, but I cannot seem to get it working.

Would appreciate any help as this is doing my head in!

Thanks

有帮助吗?

解决方案

You need to place the part where you increment the counter in the if loop:

  ...
</li>
<?php $counter++; ?>
<?php } ?>       
<?php endwhile; ?>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top