Title: Simple IF statement help
Last modified: August 19, 2016

---

# Simple IF statement help

 *  [driz](https://wordpress.org/support/users/driz/)
 * (@driz)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/)
 * Hi I have this code:
 *     ```
       if (...) {
       echo '<li>';
       previous_posts_link($pagenavi_options['prev_text'], $max_pages);
       echo '</li>';
       }
       ```
   
 * I’m wanting to add some code where it says … that will detect the previous_posts_link
   function running before running the code. This is to prevent WordPress running
   the echos when the previous_posts_link is empty ie. their isn’t a previous set
   of posts!
 * THANKS

Viewing 15 replies - 1 through 15 (of 15 total)

 *  Thread Starter [driz](https://wordpress.org/support/users/driz/)
 * (@driz)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319119)
 * any updates on this?
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319121)
 * Not familiar with page navi, but shouldn’t previous_posts_link return empty anyway
   if there aren’t any previous posts?
 *  Thread Starter [driz](https://wordpress.org/support/users/driz/)
 * (@driz)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319123)
 * Yes it should, but I have wrapped two echos around it, and so I want to make 
   sure those don’t get executed either if there aren’t any previous posts. It should
   just be a simple if but I’m not sure what the if would be…
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319130)
 * Determine if you’re on the last page..
 * Example code:
 *     ```
       if( !$wp_query ) global $wp_query;
       $paged = get_query_var('paged') ? get_query_var('paged') : 1;
       $postsinquery = $wp_query->found_posts;
       $totalpages = ceil( $postsinquery / $wp_query->query_vars['posts_per_page'] );
       ```
   
 * Then check if it’s the last page, if it is, then there’s no more posts..
 *     ```
       if( $paged == $totalpages ) {
       // ITS THE LAST PAGE
       }
       ```
   
 * or
 *     ```
       if( $paged < $totalpages ) {
       // ITS ANYTHING BUT THE LAST PAGE
       }
       ```
   
 * Easiest solution i could think of right now.
 * Does that help?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319134)
 * [This topic](http://wordpress.org/support/topic/273881?replies=3) might have 
   an answer for you.
 *  Thread Starter [driz](https://wordpress.org/support/users/driz/)
 * (@driz)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319137)
 * Think that’s gonna be too complicated and allow breaks to happen. Is their not
   a simple way to test if a function has run?
 *  Thread Starter [driz](https://wordpress.org/support/users/driz/)
 * (@driz)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319140)
 * That post in that topic might work. But it’s for previous_post rather than previous_posts
   which is a different function and has different variables etc as far as I know.
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319143)
 * $wp_query holds information on the current query, that’s why i used it in the
   example above..
 * If you want to me explain or break down something i’ve shown above, just ask…
   😉
 *  Thread Starter [driz](https://wordpress.org/support/users/driz/)
 * (@driz)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319144)
 * The solution presented by vtxyzzy is the best I think, as it will allow me to
   rewrite the links exactly how I want them. However that code is for another function,
   just tried it and nothing is spat out as its looking for next post when your 
   previewing a single rather than the next set of posts. Can anyone help in editing
   the code to work for next posts rather than next post. Thanks
 *  Thread Starter [driz](https://wordpress.org/support/users/driz/)
 * (@driz)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319146)
 * Seems their is no `get_next_posts()` function, so I’m not sure how you get the
   next set of posts.
 *  [Michael](https://wordpress.org/support/users/alchymyth/)
 * (@alchymyth)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319162)
 *     ```
       if(previous_posts_link()) {
       echo '<li>';
       previous_posts_link($pagenavi_options['prev_text'], $max_pages);
       echo '</li>';
       }
       ```
   
 *  Thread Starter [driz](https://wordpress.org/support/users/driz/)
 * (@driz)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319226)
 * That code above doesn’t work. Any other ideas of what the if statement would 
   be?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319230)
 * I think this will work:
 *     ```
       if (!$wp_query) global $wp_query;
       if ( !$wp_query->query['paged']) {
          // On first page;
       } elseif ($wp_query->max_num_pages == $wp_query->query['paged']) {
          // On last page;
       }
       ```
   
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319245)
 * Nice and simple… 🙂
 * Is max_num_pages the same as the last page for the query though? .. is the name
   just misleading..
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319281)
 * Not sure, couldn’t find any documentation. But this works for me.

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Simple IF statement help’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 15 replies
 * 4 participants
 * Last reply from: [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * Last activity: [16 years, 5 months ago](https://wordpress.org/support/topic/simple-if-statement-help/#post-1319281)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
