• Hi,

    In 404 page I would like to display a message and then display all the posts in wordpress. The below code is not working and obviously it seems that 404 is meddling with the default global query which results in zero records.

    if ( have_posts() ) : ?>
    <div>
    <?php while ( have_posts() ) : the_post(); ?>
    <div>
    <?php
    get_template_part( 'content', get_post_format() );?>
    </div>
    <?php else : ?>
    <div> <h3>No posts to show </div>
    <?php endif; ?>

    This code always go to the else block in the 404 page. This is the same code I use on the main page to display all the posts. I tried to place wp_reset_query(); to reset the query to global default but no luck in fetching all the posts.

    What am I missing here?

    Thanks
    Guna

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    The global query contains no records because the request failed to find anything, thus the 404. Thus you cannot run a global query loop, you must run a custom query and loop, like so:

    $the_query = new WP_Query( array('post_status' => 'publish',) );
    <div><ul>
    if ( $the_query->have_posts() ) : ?>
       <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
          <?php echo '<li>' . get_the_title() . '</li>'; ?>
       <?php endwhile;
    else : ?>
       <li> <h3>No posts to show</h3> </li>
    <?php endif; ?>
    </ul></div>

    Note that your code also has some syntax errors that could be confusing any output. Try starting with my code, then tweak it a little at a time to meet your specific needs, testing after each tweak. This is often preferable to writing everything at once and then having to locate errors in a mass of code.

    Thread Starter tgunasekhar

    (@tgunasekhar)

    bcworkz, thank you. I got clearity now. With some desired parameters in the query I got it working now.

    Cheers
    Guna

    Thanks you!! I got it. ^__^

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘404 Page modifications’ is closed to new replies.