• I need to check, if in the loop next post exist.
    Someone adviced me to use this:

    <?php $my_query = new WP_Query('category_name=portfolio&showposts=9999'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post(); $i++; ?>					
    
    	<?php the_title(); ?>
    	<?php if ($my_query->have_posts()) : ?>
    		<hr />
    	<?php endif; ?>		
    
    <?php endwhile; ?>

    But this solution makes the loop goes infinity (afret first pass on all posts, it goes over again).
    So what is the method to check if the next post exist, and if so, then do something (display hr in example)?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Put this after your new WP_Query

    echo "<pre>"; print_r($my_query->post_count); echo "</pre>";
    Thread Starter cyberek0

    (@cyberek0)

    I have got to this method after some try and fail approaches.
    Finally comparing $i to post_count, so it does the job as far, as i have all posts on one page. But, what if pagination start? Can i access default posts/page to compare to it?
    My solution works like this:

    <?php if( ($my_query->post_count) == $i ) : ?>
    <hr />
    <?php endif; ?>

    Don’t have 9999 posts to show a paging example….use this to look at all the query variables:

    echo "<pre>"; print_r($my_query->query_vars); echo "</pre>";

    You’ll want
    $my_query->post_per_page
    $my_query->paged

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Check if next post exist.’ is closed to new replies.