• smbaker

    (@smbaker)


    My site makes extensive use of page templates to render different classes of pages. Each page has some code that looks like the following for dealing with what happens when a page cannot be found:

    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    
    ... stuff for displaying the page ...
    
    <?php endwhile; ?>
    
    <?php else : ?>
    
    ... stuff for telling the user the page can't be found ...
    Sorry, but you are looking for something that isn't here. You can search again by using 
    
    <?php endif; ?>

    Recently I wanted to change this message, but I don’t want to have to hunt it down in 20 different page templates and keep them all consistent. I’m assuming that’s what a “404.php” template would be for…

    Now, to my question…. Are the conditions that cause 404.php and !have_posts() to be true the same? Can I safely eliminate the !have_posts() code from my page templates provided I supply a 404.php? It seems like a great way to clean up the page templates and make them simpler.

    Also, is there an action hook or a filter where I can intercept page not found errors? I’m considering writing a plugin to deal with them.

    Thanks,
    Scott

Viewing 1 replies (of 1 total)
  • esmi

    (@esmi)

    Can I safely eliminate the !have_posts() code from my page templates provided I supply a 404.php?

    Yes.

    Not sure about a filter though. There’s the is_404() conditional if that’s of any use.

Viewing 1 replies (of 1 total)
  • The topic ‘Pages templates, have_posts(), and 404.php’ is closed to new replies.