将WordPress 404更改为其他页面


Change WordPress 404 to a different page

如何更改WordPress调用404.php的位置?

基本上,我想发生的是,当WordPress找不到链接时,我希望调用另一个page.php,而不是404.php。

感谢

404.php是WordPress模板,用于在页面"未找到"时显示某些内容。

最简单的方法是将404.php模板编辑为您想要的模板(与anotherpage.php相同)

如果这不可能,您可以在404.php模板中添加以下内容,将页面重定向到您想要的位置:

<?php wp_redirect( ‘http://www.example.com/anotherpage.php’, 301 ); exit; ?>

这将把访问者发送到您想要的页面。

使用过滤器覆盖包含的模板。

示例:

function wpse_override_404_template( $template_path ) {
    return locate_template( 'template-to-override-with.php' );
}
add_filter( '404_template', 'wpse_override_404_template' );

此过滤器用于get_query_template()内部。要了解更多信息,请查看codex页面:http://codex.wordpress.org/Function_Reference/get_query_template