所以我无法找到并了解相关信息,但我想通过自定义页面模板将默认404页面挂钩到页面,以便我可以管理404页面上的内容。这可能吗?
答案 0 :(得分:0)
要执行此操作,只需在主题中创建名为404.php的页面。
看一下Twenty Thirteen附带的404.php文件的简单结构。它基本上具有显示标题,侧边栏和页脚的标签,以及您的消息的区域:
<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 -->
因此,要更改访问者看到的错误消息,请修改h1标题内和页面内容类中的文本;如有必要,请在下面添加更多段落。
您可以在WordPress Codex中看到有关此内容的更多信息。
现在要向此页面添加内容,您需要先在WP中创建一个新页面并告诉它使用您的404模板。一旦我们添加了页面内容,我们只需要在模板php文件中添加一些代码。以下假设我们的新页面的ID为&#39; 12&#39;。
<?php
// would get post 12's entire content after which you
// can manipulate it with your own trimming preferences
$post_12 = get_post(12);
$content = $post_12->post_content; //would echo post 12's content
//$excerpt = $post_12->post_excerpt; // would echo post 12's content up until the <!--more--> tag
?>
现在将此代码添加到您的模板中,如下所示:
<?php
/**
* The template for displaying 404 pages (Not Found)
*
* @package WordPress
* @subpackage Twenty_Thirteen
* @since Twenty Thirteen 1.0
*/
get_header(); ?>
$post_12 = get_post(12);
$content = $post_12->post_content; //would echo post 12's content
<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 $content ?></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 -->
<?php get_footer(); ?>
但是,假设您要在不知道页面ID的系统上安装此页面,您可以通过主题或插件使用固定名称/标题创建页面,例如:&#34 ;定制-404&#34;
然后,您可以更改模板的代码,按名称/标题调用该帖子,因为我们总是知道这个名称是什么。
$page = get_page_by_title( 'custom-404' );
//$post_12 = get_post(12);
$content = $page->post_content; //would echo post custom-404's content