【发布时间】:2018-08-29 17:59:16
【问题描述】:
我正在为 WordPress 项目使用 Timber。我正在尝试让我的自定义 404 错误页面树枝模板显示在 404 错误上。目前,我的 404 错误显示为空白屏幕。但是,当我去路线时:/404 我的模板显示。我假设我的路由方式有问题,或者我在404.php 中遗漏了一些关键逻辑。
如何让我的 twig 模板显示我的 404 错误?
functions.php中的路由代码:
Routes::map('/404', function($params){
Routes::load('routes/404.php', null, $params, 404);
});
404.php:
<?php
$context = Timber::get_context();
$post = new TimberPost();
$context['page'] = $post;
global $wp;
$url = home_url($wp->request);
$context['url'] = preg_replace('#^https?://#', '', rtrim($url, '/'));
$context['global'] = get_fields('options');
Timber::render('views/pages/404.twig', $context);
404.twig:
{% extends "layouts/layout.twig" %}
{% block content %}
<div class="row row-is-centered">
<div class="col ns-col-is-8 lead-6 txt-center">
<h1 class="txt-size-5">Our apologies, we couldn't find {{url}}</h1>
</div>
</div>
{% endblock %}
【问题讨论】:
-
检查您的 404.php 的放置位置。如果它不在主题的根文件夹中,它可能不起作用。另外,请查看@robertguss 的答案。他对路由问题有一些很好的指导。