Leave a Reply

2 comments

  1. The 404.php template file is used for 404 errors: i.e. no posts found.

    Since there are no posts, there is no $post object. With no $post object, functions such as the_ID() are not available, and will return the error you’re observing.

    The fix: replace all instances of $post-derived data with static data. e.g. replace this:

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    

    …with this:

    <div id="post-0" <?php post_class(); ?>>
    
  2. the_ID is meant for use in the Loop. Even if your Loop starts in get_header not everything is going to have proper loop. It kinda looks like that what is happening, but on the other hand have_posts, which should start your Loop, should mean the Loop doesn’t start on that 404 page. That is what happened when I tried it, anyway. So maybe this page just isn’t in a Loop at all.

    $post is set very early in page loads most of the time. If this isn’t in a Loop– that is, the Loop doesn’t start in get_header— that ID may not be what you expect, or what you want. On a blog index, index.php, that ID is the ID of the first post in the loop, not really the ID of the index page, which doesn’t have an ID at all unless you’ve set the index to a page in the backend. Even then, though, the ID is the first post in the Loop, not the ID of the ‘static’ page. You will have the same post/Loop problem with $post->ID.

    And, of course, most of this is based on guessing at some of your theme/template structure.

    Your really should be setting it conditionally like…

    if (is_single() {
        $pID = $post->ID;
    } elseif (is_search()) {
        $pID = <something>;
    } // and so on until you get all the conditions you need
    
    <div id="content" style="full-width">
        <div id="post-<?php echo $pID ?>" <?php post_class(); ?>>
    

    Also, I am pretty sure ids are not supposed to start with a digit. I’d have to look it up, but if memory serves./…