Forums

[resolved] custom 404 messages depending on path? (5 posts)

  1. amenitya
    Member
    Posted 3 years ago #

    I would like to have several different 404 error messages that display according to the url path they appear in.
    For example, here is a search for recipes that returns no posts:
    http://protogeo.info/wordpress_v2/recetas/resultados?prod=37
    I would like to change the error message to something more specific, and assumed I could do this by using conditional tags in my 404.php file, like this:

    <?php
      if is_page_template('page-recipe-results.php') {
          _e('No recipes match that category.','k2_domain');</p>
      } else {
          _e('Oh no! You\'re looking for something which just isn\'t here!','k2_domain');</p>
      }
    ?>

    However, that didn't work. I also considered that I might parse the directory from the URL path so that any 404 pages preceded by "http://protogeo.info/wordpress_v2/recetas/resultados" would return the recipe-specific statement, yet I couldn't get any value using $_SERVER['PHP-SELF'].
    Maybe there's a better approach I'm missing?

  2. helpshop.com
    Member
    Posted 3 years ago #

  3. amenitya
    Member
    Posted 3 years ago #

    I'll try it- thanks. But surely there is a non-plug-in solution to this???

  4. amenitya
    Member
    Posted 3 years ago #

    Nope, doesn't work. I'm not trying to redirect from static URLs, but rather URLs that are passing a parameter to query_posts:
    http://protogeo.info/wordpress_v2/recetas/resultados/?prod=27
    Should not redirect if there are posts in category 27.
    I think maybe the solution involves the query itself rather than the 404 page..

  5. amenitya
    Member
    Posted 3 years ago #

    Okay, it's embarrassing how simple the solution was, but I'll post it here in case anyone else is every as daft as me :). Just catch a query that returns no posts before entering the loop:
    <?php if (have_posts()){
    include (TEMPLATEPATH . '/theloop.php');
    }
    else {
    print "nope";
    }?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.