WordPress.org

Ready to get started?Download WordPress

Forums

envoke 404 manualy (7 posts)

  1. shazdeh
    Member
    Posted 4 years ago #

    I'm using custom url in my theme and have made my own template hiracical (i can't spell it!).
    So, in some situations I want to tell the wp that a 404 error happens, so use the 404.php to make the page. how to do that?

  2. Robert Chapin
    Member
    Posted 4 years ago #

    I'd like to know this one too!

  3. Shane G
    Member
    Posted 4 years ago #

    Hi,

    Set your desired permalink and add this code in htaccess:

    `# BEGIN WordPress

    <IfModule mod_rewrite.c>
    ErrorDocument 404 /index.php?error=404
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>

    # END WordPress`

    Thanks,

    Shane G.

  4. Robert Chapin
    Member
    Posted 4 years ago #

    Hi Shane, it seems you've missed the point of the OP. He wants to throw 404 instead of echoing "Not Found" in the template's "Else" case.

    From what I've seen so far, it looks like this can only be done at the top of the template before any whitespace and before get_header(). The template has to call has_posts() and then handle the 404 manually by setting headers, including other templates, and quitting the script.

  5. Robert Chapin
    Member
    Posted 4 years ago #

    For example, at the top of archive.php, get rid of this:
    <?php get_header(); ?>

    Replace with this:

    <?php
    if (!have_posts()) {
        header("HTTP/1.0 404 Not Found - Archive Empty");
        require TEMPLATEPATH.'/404.php';
        exit;
    }
    get_header();
    ?>

    NOW you have proper SEO. It's shocking to see the WordPress core miss things like that.

  6. Robert Chapin
    Member
    Posted 4 years ago #

    This works a little better:

    <?php
    if (!have_posts()) {
        header("HTTP/1.0 404 Not Found - Archive Empty");
        $wp_query->set_404();
        require TEMPLATEPATH.'/404.php';
        exit;
    }
    get_header();
    ?>
  7. Rahul Sonar
    Member
    Posted 4 years ago #

    miqrogroove

    Yes, you are absolutely correct. We use it like this:

    if(have_posts())
    {
    .. do something
    }
    else
    {
    include(TEMPLATEPATH . '/404.php');
    }

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags