[英]How can I use the block editor for a 404 page in WordPress without redirecting from /404
I have a WordPress website and a custom theme, and of course a 404.php
file.我有一个 WordPress 网站和一个自定义主题,当然还有一个
404.php
文件。 I have followed this amazing guide on making the 404 page template editable with the block editor.我遵循了这个惊人的指南,让 404 页面模板可以使用块编辑器进行编辑。
My components on the 404 page appear correctly when I go to /404
(or for that matter, any invalid link), however, when going to /404
, the user is redirected to /404-2
This is the behavior I would like to fix.当我 go 到
/404
(或就此而言,任何无效链接)时,我在 404 页面上的组件显示正确,但是,当转到/404
时,用户被重定向到/404-2
这是我想要的行为使固定。 When the user goes to /abc
(an invalid page) then the 404 is shown and they ae not redirected and still on /abc
(which is as intended).当用户转到
/abc
(无效页面)时,会显示 404 并且它们没有被重定向并且仍然在/abc
上(这是预期的)。
I checked the page in WP and the URL slug is set to /404-2
and attempting to change it to 404 just defaults back to 404. It seems that this is due to 404 being a number and default WP behavior .我检查了 WP 中的页面,并且 URL slug 设置为
/404-2
并尝试将其更改为 404 只是默认返回 404。这似乎是由于404 是一个数字和默认的 WP 行为。 But this just tells me why the problem occurs;但这只是告诉我为什么会出现问题; I do not know how to override or correct this behavior.
我不知道如何覆盖或纠正这种行为。 Thanks in advance.
提前致谢。
As you've found, the page title "404" (or any numeric title) will always be rewritten to "404-2", this ensures the title is distinct/never confused with a numeric post/page id.正如您所发现的,页面标题“404”(或任何数字标题)将始终被重写为“404-2”,这确保标题是不同的/永远不会与数字帖子/页面 ID 混淆。
A simple solution would be to rename the page to "404 page" and update the php function to get_page_by_title( '404 page' )
.一个简单的解决方案是将页面重命名为“404 page”并将 php function 更新为
get_page_by_title( '404 page' )
。 This would give your page more SEO friendly url of /404-page
and not require trying to change permalinks/rewriting of the .htaccess file.这将使您的页面对
/404-page
的 url 更加 SEO 友好,并且不需要尝试更改.htaccess文件的永久链接/重写。
An alternative to consider is migrating your custom theme to a block theme if using WordPress 5.9 or higher.如果使用 WordPress 5.9 或更高版本,可以考虑将自定义主题迁移到块主题。 This would enable you to create a true "404" page template using the Block Editor that can be edited via the
Site Editor > Templates
.这将使您能够使用可以通过
Site Editor > Templates
编辑的块编辑器创建真正的“404”页面模板。 The Site Editor enables you to add new templates , save and export them for easily creating your own templates for your custom theme. 站点编辑器使您能够添加新模板、保存和导出它们,以便为您的自定义主题轻松创建您自己的模板。 Starting with a base theme like Twenty Twenty Two is a good way to see what is possible.
从像二十二十二这样的基本主题开始是了解可能的好方法。 It's very different to creating a "classic" php-based theme;
这与创建“经典”基于 php 的主题非常不同; though the advantage of block templates and the Site Editor is a great benefit for future-proofing your theme.
尽管块模板和站点编辑器的优势对于将来验证您的主题是一个很大的好处。
Fixed it by hooking onto do_redirect_guess_404_permalink
通过挂钩
do_redirect_guess_404_permalink
来修复它
See https://developer.wordpress.org/reference/hooks/do_redirect_guess_404_permalink/请参阅https://developer.wordpress.org/reference/hooks/do_redirect_guess_404_permalink/
<?php
function stop_redirect_guess() {
return false;
}
add_filter( 'do_redirect_guess_404_permalink', 'stop_redirect_guess');
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.