ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
如果用户绊倒不存在或尚未创建的页面,则404页面对于添加到主题中很重要。 同样重要的是,您的404页面为您的访问者提供了到达正确位置的方法。 接下来的几个步骤将帮助您为您的WordPress主题构建一个有用的404页面。 ## 创建404.php文件 由于404.php文件用于未找到页面的错误,所以第一步是创建一个名为404.php的文件,并将其添加到您的WordPress主题文件夹中。 >[info] 提示:您的404.php通常是您的index.php的一个很好的起点 ## 添加文件头 要使用您的主题的header.php文件,请打开您的404.php文件。 在顶部,添加一个描述该文件的注释,并确保包含您的主题标题。 例如: ``` /** * The template for displaying 404 pages (Not Found) */ get_header(); ``` ## 将身体添加到您的404页面 要使404页面功能正常,您必须将正文内容添加到模板中: 例: ``` <div id="primary" class="content-area"> <div id="content" class="site-content" role="main"> <header class="page-header"> <h1 class="page-title"><?php _e( 'Not Found', 'twentythirteen' ); ?></h1> </header> <div class="page-wrapper"> <div class="page-content"> <h2><?php _e( 'This is somewhat embarrassing, isn’t it?', 'twentythirteen' ); ?></h2> <p><?php _e( 'It looks like nothing was found at this location. Maybe try a search?', 'twentythirteen' ); ?></p> <?php get_search_form(); ?> </div><!-- .page-content --> </div><!-- .page-wrapper --> </div><!-- #content --> </div><!-- #primary --> ``` 这个例子来自WordPress的Twenty Thirteen主题。 这在404页面中添加了一些文本和搜索表单。 >[warning] 注意:您可以添加自己的文本,使您的404页面看起来更好。 ## 添加页脚和侧边栏。 创建404页面的最后一步是添加页脚。 如果需要,您还可以添加侧边栏。 例: ``` get_sidebar(); get_footer(); ``` 现在,您有一个带有搜索表单和一些文本的功能404页面,以防用户绊倒错误的页面。 一旦你确定404.php已经上传,你可以测试你的404页面。 只需键入不存在的网站的URL地址。 您可以尝试像http://example.com/fred.php。 这确实会导致404错误,除非你实际上有一个名为fred.php的PHP文件。