wordpressblog-post

have_post() does return null value in wp


I used the following code in my page template:

<?php
while(have_posts()):the_post();
the_content();
?>

But nothing is displayed. The loop is not working. I'm sure that, there is sufficient information as content in my template page.


Solution

  • You should use if condition to check if post exists else skip the loop. Make sure to ON the error log and check the exact error.

    <?php wp_reset_query(); ?>
    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php the_content(); ?>
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>