WordPress.org

Ready to get started?Download WordPress

Forums

Simple IF statement help (16 posts)

  1. driz
    Member
    Posted 4 years ago #

    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

  2. driz
    Member
    Posted 4 years ago #

    any updates on this?

  3. Mark / t31os
    Moderator
    Posted 4 years ago #

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

  4. driz
    Member
    Posted 4 years ago #

    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...

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    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?

  6. vtxyzzy
    Member
    Posted 4 years ago #

    This topic might have an answer for you.

  7. driz
    Member
    Posted 4 years ago #

    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?

  8. driz
    Member
    Posted 4 years ago #

    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.

  9. Mark / t31os
    Moderator
    Posted 4 years ago #

    $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... ;)

  10. driz
    Member
    Posted 4 years ago #

    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

  11. driz
    Member
    Posted 4 years ago #

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

  12. alchymyth
    The Sweeper & Moderator
    Posted 4 years ago #

    if(previous_posts_link()) {
    echo '<li>';
    previous_posts_link($pagenavi_options['prev_text'], $max_pages);
    echo '</li>';
    }
  13. driz
    Member
    Posted 4 years ago #

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

  14. vtxyzzy
    Member
    Posted 4 years ago #

    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;
    }
  15. Mark / t31os
    Moderator
    Posted 4 years ago #

    Nice and simple... :)

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

  16. vtxyzzy
    Member
    Posted 4 years ago #

    Not sure, couldn't find any documentation. But this works for me.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.