0

There is 404.php in the theme uploaded. But when there is search for non-existent page. The 404.php does not show up. What is the reason?

Assistance required.

  • I got it and updated my site. if somebody was going to comment. thanks. – Raghav Jan 5 '15 at 10:49
  • What do you mean with "search for non-existent page"? – cybmeta Jan 5 '15 at 11:36
  • I meant searching a page, which is not present in my domain/website – Raghav Jan 5 '15 at 12:21
  • You still are saying the same. Searching a page, where? In the standar seacrh form? Searching a page title? Searching a term that return zero results? Using a URL for a page that doesn't exist? What do you mean? – cybmeta Jan 5 '15 at 12:44
1

There is 404.php in the theme uploaded. But when there is search for non-existent page. The 404.php does not show up.

When there is search for non-existent page doesn't produce the 404 page, but simply shows the search.php's else portion. A simple search.php is like:

<?php if( have_posts() ) : ?>
   <?php while( have_posts() : the_post(); ?>
      <div><?php the_excerpt(); ?></div>
   <?php endwhile;
<?php else : ?>
   <div><?php _e( "No results found", "text-domain" ); ?></div>
<?php endif; ?>

A search for non-existent pages will show the else part only.

A 404 will generate when something is browsed through URL and that doesn't exist at all. So at that time the 404.php will be echoed.

If you want to show the else part a more stylish like 404, design the else part as per your demand. :) And if you want the user to redirect to the 404 page (I don't know why someone can want this!), you can also that like the following (in search.php):

<?php else : ?>
   <?php
   wp_redirect( home_url('/kauwa') ); //kauwa - garbage text that won't produce a real page
   exit;
   ?>
<?php endif; ?>

But as I said, it's not a good practice, you know.

  • Thank you for that Mayeenul Islam. I would implement that. – Raghav Jan 5 '15 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.