How to determine if I’m on the first page of pagination?
Common question for coders ,How do I determine if I’m on the very first page of pagination?
Many a times coders want to run a function or code some functionality only on the first page of the pagination, following code will help you to achieve this.
1 2 3 4 5 6 |
// get current page we are on. If not set we can assume we are on page 1. $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // are we on page one? if(1 == $paged) { //true } |
Now taking this further you want to add some functionality only for some posts, following code will help you.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?php if ( have_posts() ) : $which_post_being_showed = 0; // The Loop while ( have_posts() ) : the_post(); $which_post_being_showed++; ?> //......some code..... <?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // are we on page one? if( (1 == $paged ) && ($which_post_being_showed==2 || $which_post_being_showed==4)) : ?> //.....some code....... <?php endif; ?> //......some code..... <?php endwhile; // End Loop else: ?> //......some code if no posts..... <?php endif; ?> |
Leave a Reply
Be the First to Comment!