如何在没有插件的情况下在管理页面的 Wordpress 中自定义 404 页面?
How can I customize 404 page in Wordpress in admin page without plugins?
所以,我在主题文件夹中创建了 404.php 页面,现在想显示应该可以在管理页面中编辑的内容。我在 google 上找到的每条建议都说我需要插件,但我只想显示内容字段,仅此而已。我不想为这种微不足道的事情安装插件。我该怎么做?
更新:
忘记提及 404 页面使用其他页面也使用的代码块,并且使用 the_field
函数读取这些页面和 404 页面的自定义字段。如果我创建另一个 404 Content
页面,这意味着我不能重用这些块,因为它们读取 this 页面的字段,而不是 404 Content
.
404.php
@include 'header.php'
...other code
header.php
<html lang="ru">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="build/css/<?php the_field('style') ?>Styles.css">
<?php wp_head(); ?>
</head>
<body>
因此每个内容页面都有自定义字段,其中包含应加载的 css 文件的名称。如果我使用 404 Content
页面,这意味着我不能重复使用 header.php
文件,对吗?
您当然不需要插件,有几种方法可以做到这一点。最简单的“捷径”方法是创建一个单独的“404 内容”页面,获取该页面的 ID,然后使用 get_post()
or get_page_by_title()
将内容导入您的 404.php
文件,如下所示:
404.php:
<?php
/**
* Handle 404 errors with this file
*/
<!-- Some markup and/or functions here, get_header() etc. -->
// Display Page Content
4_page = get_page_by_title( '404 Content' ); // Or use $page = get_post(123) where 123 is the ID of your 404 Content Page
echo apply_filters( 'the_content', 4_page->post_content );
<!-- Some markup and/or functions here, get_footer() etc. -->
?>
或者,您可以使用 is_404()
函数以不同方式处理 404 页面,但由于您已经自定义了 404.php
,因此上述方法可能是最简单的方法。
更新:
由于您的通用 header.php
文件需要 ACF 的 the_field()
函数,您可能需要稍微重构一下。您会在文档中注意到它允许 3 个参数,第二个是 $post_id
.
您可以在 header.php
文件中使用如下内容动态修改 ID:
if( is_404() ){
$page = get_page_by_title( '404 Content' );
$id = $page->ID;
} else {
$id = get_the_ID();
}
然后将您对 the_field( 'some_field' )
的调用更新为 the_field( 'some_field', $id )
所以,我在主题文件夹中创建了 404.php 页面,现在想显示应该可以在管理页面中编辑的内容。我在 google 上找到的每条建议都说我需要插件,但我只想显示内容字段,仅此而已。我不想为这种微不足道的事情安装插件。我该怎么做?
更新:
忘记提及 404 页面使用其他页面也使用的代码块,并且使用 the_field
函数读取这些页面和 404 页面的自定义字段。如果我创建另一个 404 Content
页面,这意味着我不能重用这些块,因为它们读取 this 页面的字段,而不是 404 Content
.
404.php
@include 'header.php'
...other code
header.php
<html lang="ru">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="build/css/<?php the_field('style') ?>Styles.css">
<?php wp_head(); ?>
</head>
<body>
因此每个内容页面都有自定义字段,其中包含应加载的 css 文件的名称。如果我使用 404 Content
页面,这意味着我不能重复使用 header.php
文件,对吗?
您当然不需要插件,有几种方法可以做到这一点。最简单的“捷径”方法是创建一个单独的“404 内容”页面,获取该页面的 ID,然后使用 get_post()
or get_page_by_title()
将内容导入您的 404.php
文件,如下所示:
404.php:
<?php
/**
* Handle 404 errors with this file
*/
<!-- Some markup and/or functions here, get_header() etc. -->
// Display Page Content
4_page = get_page_by_title( '404 Content' ); // Or use $page = get_post(123) where 123 is the ID of your 404 Content Page
echo apply_filters( 'the_content', 4_page->post_content );
<!-- Some markup and/or functions here, get_footer() etc. -->
?>
或者,您可以使用 is_404()
函数以不同方式处理 404 页面,但由于您已经自定义了 404.php
,因此上述方法可能是最简单的方法。
更新:
由于您的通用 header.php
文件需要 ACF 的 the_field()
函数,您可能需要稍微重构一下。您会在文档中注意到它允许 3 个参数,第二个是 $post_id
.
您可以在 header.php
文件中使用如下内容动态修改 ID:
if( is_404() ){
$page = get_page_by_title( '404 Content' );
$id = $page->ID;
} else {
$id = get_the_ID();
}
然后将您对 the_field( 'some_field' )
的调用更新为 the_field( 'some_field', $id )