$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>redirect - Dynamic 404 page content while still keeping 404 status code?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

redirect - Dynamic 404 page content while still keeping 404 status code?

matteradmin10PV0评论

I used Divi to build the layout for a custom 404 page. I'm using this plugin / to redirect 404 errors to this page. It works, but when I run tools like screaming frog, it returns broken pages as 301 redirects, which I guess is expected.

What I would prefer however is for my 404.php template to include the page I built, so that I can still accurately track my 404 errors. I'm thinking of something like:

#404.php
<?php 

require(/*Some function to get my entire custom 404 page by slug or ID*/;)

?>

What I tried:

//Redirect to our custom 404 page
function wf_404(){
//Check if custom 404 page exists to protect against infinite loop
   if (is_404() && get_page_by_path('/404-page/', OBJECT)){
       wp_safe_redirect(get_site_url() . '/404-page/', 404);
       exit;
   } 
}

add_action('get_header', 'wf_404');

This gives me a general browser 404 error.

I used Divi to build the layout for a custom 404 page. I'm using this plugin https://wordpress/plugins/redirect-404-error-page-to-homepage-or-custom-page/ to redirect 404 errors to this page. It works, but when I run tools like screaming frog, it returns broken pages as 301 redirects, which I guess is expected.

What I would prefer however is for my 404.php template to include the page I built, so that I can still accurately track my 404 errors. I'm thinking of something like:

#404.php
<?php 

require(/*Some function to get my entire custom 404 page by slug or ID*/;)

?>

What I tried:

//Redirect to our custom 404 page
function wf_404(){
//Check if custom 404 page exists to protect against infinite loop
   if (is_404() && get_page_by_path('/404-page/', OBJECT)){
       wp_safe_redirect(get_site_url() . '/404-page/', 404);
       exit;
   } 
}

add_action('get_header', 'wf_404');

This gives me a general browser 404 error.

Share Improve this question edited Feb 13, 2019 at 17:45 user3183717 asked Feb 13, 2019 at 17:38 user3183717user3183717 2313 silver badges12 bronze badges 1
  • The hacky way to get there would be to have your get_header() call in your 404 template like any other page and then use the page structure from your regular page template file and instead of using the loop just echo apply_filters('the_content', $page->post_content); where $page is the results of get_post('ID_of_your_page'). It's not ideal as it requires hard coding the page ID into your template but it will get the job done if no one else has a better answer. – mrben522 Commented Feb 13, 2019 at 17:56
Add a comment  | 

1 Answer 1

Reset to default 1

You could copy the php/html from your singular.php/page.php to 404.php. Then replace the content loop with echo apply_filters('the_content', $page->post_content); as mrben522 suggested.

Using the code from codex as an example,

<?php
/**
 * The template for displaying 404 pages (Not Found)
 *
 * @package WordPress
 * @subpackage Twenty_Thirteen
 * @since Twenty Thirteen 1.0
 */

get_header(); ?>

<div id="primary" class="content-area">
    <div id="content" class="site-content" role="main">

        <header class="page-header">
            <h1 class="page-title"><?php _e( 'Not Found', 'twentythirteen' ); ?></h1>
        </header>

        <div class="page-wrapper">
            <div class="page-content">
                <?php 
                    $page_id = get_post(100); // change ID;
                    echo apply_filters('the_content', $page->post_content); // This is where the magic (should) happen
                ?>
            </div><!-- .page-content -->
        </div><!-- .page-wrapper -->

    </div><!-- #content -->
</div><!-- #primary -->
Post a comment

comment list (0)

  1. No comments so far