admin管理员组

文章数量:1323323

I have a 404 page in my theme but I am not using that page. I have created a new 404 page in WordPress using wpbakery page builder. I need to know how can I redirect users on the new 404 page without a plugin?

I have a 404 page in my theme but I am not using that page. I have created a new 404 page in WordPress using wpbakery page builder. I need to know how can I redirect users on the new 404 page without a plugin?

Share Improve this question asked Aug 28, 2020 at 5:51 Naren VermaNaren Verma 2491 gold badge6 silver badges19 bronze badges 2
  • Do you mean you want to use your new wpbakery page as the 404 page? I'm not sure implementing that as a redirect is a good idea as search engines etc. will see the redirect status code and not the missing page. – Rup Commented Aug 28, 2020 at 8:56
  • 1 The term "redirect" is commonly misused in this context. You probably mean you want to serve your new 404 page instead of the current one. (A "redirect" implies an HTTP 3xx external redirect - which is generally a bad idea when serving an error document, as @Rup has already pointed out.) – MrWhite Commented Aug 28, 2020 at 9:27
Add a comment  | 

1 Answer 1

Reset to default 1

You can assign the 404.php as a template to your custom 404 page created with the page builder.
You would need to edit the 404.php to something like this:

<?php

/**
 * Template Name: 404
 */

get_header();

$query = new WP_Query([
    'posts_per_page' => 1,
    'post_type' => 'page',
    'meta_key' => '_wp_page_template',
    'meta_value' => basename(__FILE__)
]);

if ($query-> have_posts()) {
    echo do_shortcode($query->post->post_content)
}

get_footer();

This way you would load the contents of your custom 404 page into the 404.php template.
You probably need to adapt the code above to the actual HTML structure of your theme.

本文标签: htaccessHow can I redirect users on the new 404 page without plugin