• erbanach

    (@erbanach)


    I am trying to get my custom 404 error page twig template to display on 404 errors. Currently, my 404 errors are showing a blank screen. However, when I go to the route: /404 my template displays. I am assuming there is either something wrong with how I am routing or I am missing some key logic in 404.php.

    How can I get my twig template to display for my 404 errors?

    Route code in 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 %}
    • This topic was modified 7 years ago by erbanach.
Viewing 1 replies (of 1 total)
  • Hey there

    Usually, you don’t need routes to display 404 pages. It’s enough if you provide a 404.php file in the root folder of your theme. So if you have the 404.php file in a subfolder called “routes”, it won’t work. Try moving the 404.php into the root of your theme and delete the routes in your functions.php. I’d say it should work then.

    Otherwise, you can check out how it’s done in the Starter Theme.

Viewing 1 replies (of 1 total)
  • The topic ‘Blank 404 error page’ is closed to new replies.