繁体   English   中英

Wordpress中如何将404 not found页面重定向到主页

[英]How to redirect 404 not found page to the home page in Wordpress

我有一个我不想使用的 404 错误页面。 相反,我想让 404 页面重新加载到我的主页。 我在 404 php 文件的开头插入了以下行:

<?php
/* Redirect browser */

echo '<script type="text/javascript">
           window.location = "http://www.google.com/"
      </script>';

/* Make sure that code below does not get executed when we redirect. */
exit;
?>

这有效(它将加载到我的主页。但是,新页面将重新加载并打开一个新标签页。

我想让页面重新加载,而不是打开一个新标签。

我怎样才能做到这一点?

预先感谢

您可以使用is_404 Docs function 并将其挂在template_redirect Docs挂钩中。 如果它返回 true,您可以使用wp_safe_redirect Docs function 重定向到主页/首页。

add_action('template_redirect', 'redirecting_404_to_home');

function redirecting_404_to_home()
{
    if (is_404()) 
    {
        wp_safe_redirect(site_url());
        exit();
    }
};

代码进入functions.php文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM