如何解决没有插件,如何在新的404页面上重定向用户?
我的主题中有一个404页面,但我没有使用该页面。我已经使用wpbakery页面构建器在WordPress中创建了一个新的404页面。我需要知道如何在没有插件的情况下在新的404页面上重定向用户?
解决方法
-
在管理员中创建404页面。
-
为该页面创建自定义页面模板。
-
添加自定义404内容
-
以您的主题打开404.php文件。
-
将此下面的代码添加到该文件的顶部。
header("HTTP/1.1 301 Moved Permanently"); header("Location: ".home_url('/404page/')); exit();
-
尝试查找未找到的内容,您将被重定向到自定义404页面
也,您可以尝试使用此操作挂钩来重定向到自定义404页面。将此代码放在您的function.php文件中。这是以上第5点的替换选项)
add_action( 'template_redirect','redreict_to_custom_404_page' );
function redreict_to_custom_404_page(){
// check if is a 404 error
if( is_404() ){
wp_redirect( home_url( '/404page/' ) );
exit();
}
}
或者如果您想使用WP面包店创建404页面
-
创建一个私有404页面,并在管理员中使用WPakery构建。
-
打开404.php文件,并通过下面的代码获取404page的内容
$page_id = 123; // 404page id $page = get_post( $page_id ); $content = $page->post_content; echo $content;
您可以使用插件404page。
或一些代码adapted from this plugin:
add_filter(
'404_template',static function () {
global $wp_query;
$wp_query = new WP_Query();
$wp_query->query('page_id='.$pageID);
$wp_query->the_post();
$template = get_page_template();
rewind_posts();
add_filter(
'body_class',static function ($classes) {
if (!in_array('error404',$classes,true)) {
$classes[] = 'error404';
}
return $classes;
}
);
return $template;
},999
);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。