Not exactly a page template, the ‘404.php’ file is used as the landing page for any post or page query that does not exist on the website.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | <?php get_header(); ?> <div id=“primary”> <div id=“content” role=“main”> <header class=“page-header”> <div class=“post-headline”><h1>Page Not Found</h1></div> </header> <?php global $wp; $parts = preg_split( “#[\|/]#”, preg_replace( “/(.*).(html|htm|php|asp|aspx)$/”, “”, $wp->request ) ); /* * Category or tag entry * Search categories or tags for possible matches on names and descriptions */ if ( in_array( $parts[ 0 ], array( ‘category’, ‘tag’ ) ) ) { echo ‘<h2>’ . ( ( ‘category’ === $parts[ 0 ] ) ? ‘Category’ : ‘Tag’ ) . ’ Search</h2>’; if ( 2 !== count( $parts ) ) { echo ‘<p>The’ . ( ( ‘category’ === $parts[ 0 ] ) ? ’ category ’ : ’ tag ’ ) . ‘search parameter entered is either missing or not in the correct format. Only one’ . ( ( ‘category’ === $parts[ 0 ] ) ? ’ category ’ : ’ tag ’ ) . ‘search parameter is valid in this situation.</p>’; } else { // Get all category or tag information into array if ( ‘category’ === $parts[ 0 ] ) { $taxonomy = ‘category’; } else { $taxonomy = ‘post_tag’; } $tags = get_terms( $taxonomy, array( ‘hide_empty’ => ‘1’, ‘search’ => str_replace( “_”, “-”, $parts[ 1 ] ), ) ); // Display array elements if ( $tags ) { echo ‘<p>Unfortunately, the’ . ( ( ‘category’ === $parts[ 0 ] ) ? ’ category ’ : ’ tag ’ ) . “ ‘” . $parts[ 1 ] . “ ‘” . ’ you were searching for does not exist on thie website. The following is a list of similar’ . ( ( ‘category’ === $parts[ 0 ] ) ? ’ categories ’ : ’ tags ’ ) . ‘that might correspond to what you were searching for.</p>’; echo ‘<ul>’; foreach ( $tags as $tag ) { echo ‘<li>’; echo ‘<a href=“ ‘ . attribute_escape( get_term_link( $tag, $taxonomy ) ) . ’ ”>’ . $tag->name . ‘</a> (‘ . intval( $tag->count ) . ’)’; if ( ” !== $tag->description ) echo ‘<br/>’ . $tag->description; echo ‘</li>’; } echo ‘</ul>’; } else { echo “<p>Unfortunately, there doesn’t seem to be any” . ( ( ‘category’ === $parts[ 0 ] ) ? ’ categories ’ : ’ tags ’ ) . ’ that match your request. Below is a list of all’ . ( ( ‘category’ === $parts[ 0 ] ) ? ’ categories ’ : ’ tags ’ ) . ‘for this website. Hopefully, you can find something there close to what you are looking for.</p>’; echo ‘<ul>’; foreach ( get_terms( $taxonomy, array( ‘hide_empty’ => ‘1’, ) ) as $tag ) { echo ‘<li>’; echo ‘<a href=“ ‘ . attribute_escape( get_term_link( $tag, $taxonomy ) ) . ’ ”>’ . $tag->name . ‘</a> (‘ . $tag->count . ’)’; if ( ” !== $tag->description ) echo ‘<br/>’ . $tag->description; echo ‘</li>’; } echo ‘</ul>’; } } } /* * Blog entry — uses the year/month/date/name formatting * Check for match on name * Check for match on date */ else if ( is_numeric( $parts[ 0 ] ) ) { echo ‘<h2>Post Search</h2>’; /* * If it came here with only a year, * then there was no posts for that year, * so diaplay an error message of invalid or empty date/year */ if ( 1 === count( $parts ) ) { /* * Test for valid date: returns (true) if date was valid so display the ‘not found’ message */ if ( avp_test_dates( $parts[ 0 ] ) ) { echo ‘<p>There were no posts for the given year.</p>’; } } /* * If it came here with only a year/month combo * then there was no posts for that year and month * so diaplay an error message of invalid or empty year and month */ else if ( 2 === count( $parts ) ) { if ( avp_test_dates( $parts[ 0 ], $parts[ 1 ] ) ) { echo ‘<p>There were no posts for the given year and month.</p>’; } } /* * If it came here with only a year/month/day combo * then there was no posts for that year/month/day * so diaplay an error message of invalid or empty year/month/day */ else if ( 3 === count( $parts ) ) { if ( avp_test_dates( $parts[ 0 ], $parts[ 1 ], $parts[ 2 ] ) ) { echo ‘<p>There were no posts for the given year, month or date.</p>’; } } /* * If it came here with only a year/month/date/name combo * then there was no posts for that year/month/day of the given name * so diaplay an error message of invalid or empty year/month/day/name * possibly wrong date or wrong name so check for both */ else if ( 4 === count( $parts ) ) { if ( avp_test_dates( $parts[ 0 ], $parts[ 1 ], $parts[ 2 ] ) ) { // Show any blog post that matches the name $posts = get_posts( array( “name” => $parts[ 3 ], “post_type” => “post” ) ); echo ‘<p>There were no posts for the given year.</p>’; } } } /* * About the only other thing it can be is a page */ else { echo ‘<h2>Page Search</h2>’; echo ‘<p>The requested page does not exist on this website.</p>’; for ( $links = array(), $index = ( count( $parts ) - 1 ); 0 <= $index; – $index ) { $the_query = new WP_Query( array( ‘name’ => $parts[ $index ], ‘post_type’ => ‘page’, ) ); if ( $the_query ) { while ( $the_query->have_posts() ) { $the_query->the_post(); $links[] = ‘<li><a href=“ ‘ . get_permalink() . ’ ”>’ . get_the_title() . ‘</a></li>’; } } wp_reset_postdata(); } if ( 0 === count( $links ) ) { echo ‘<p>No pages were found scanning the parent pages of the given child page.</p>’; } else { echo ‘<p>The following ’ . count( $links ) . ’ pages were found scanning the parent pages of the given child page.</p>’; echo ‘<ul>’; foreach ( $links as $link ) { echo $link; } echo ‘</ul>’; } } ?> </div><!– #content –> </div><!– #primary –> <?php get_sidebar(); get_footer(); ?> |
The page here attempts to make sense of the unfound post or page, based on the parameters of the URL.
Category / Tag Search
If the invalid URL refers to a category or tag, the page will attempt to find any category or tag that is similar to the given category or tag.
Pages
If the given page is not found, the parent pages and subpages listed for the given page are searched to find any matching pages. Matching pages are reported, otherwise an error message is returned.
Posts
If the given post is not found, an error message is returned, either stating that the given date was out of range for the published posts or that there were no posts for the given year, year and month or year, month and date. (WordPress will present any given posts if the given post parameter does match existing posts by the given date parameter or parameters.)
Personal Links
My Amazon Wishlist
My DeviantArt Gallery
My Facebook Page
My Goodreads Bookshelf
My Smashwords Catalog
My Twitter Feed
My WordPress Profile