Support » Fixing WordPress » 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!


Viewing 15 replies - 1 through 15 (of 15 total)
  • any updates on this?

    Not familiar with page navi, but shouldn’t previous_posts_link return empty anyway if there aren’t any previous posts?

    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…

    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 ) {


    if( $paged < $totalpages ) {

    Easiest solution i could think of right now.

    Does that help?

    This topic might have an answer for you.

    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?

    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.

    $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… 😉

    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

    Seems their is no get_next_posts() function, so I’m not sure how you get the next set of posts.



    Forum Moderator

    if(previous_posts_link()) {
    echo '<li>';
    previous_posts_link($pagenavi_options['prev_text'], $max_pages);
    echo '</li>';

    That code above doesn’t work. Any other ideas of what the if statement would be?

    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;

    Nice and simple… 🙂

    Is max_num_pages the same as the last page for the query though? .. is the name just misleading..

    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.