Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Rendering the right Twig template

Koen edited this page Jul 31, 2019 · 4 revisions

Since we're using Twig templates, we'll need to instruct WordPress to look for the right Twig templates. This means we'll use the lookup system specified by the WordPress template hierarchy to fetch the right PHP file. In that PHP file we'll specify the right Twig template.

Here's an example of single.php. This will first check if there's a specific template for the requested post type, and then fall back to single.twig if there isn't:

$context = Timber::get_context();
$context['templates'] = [
    'single-' . $post->post_name . '.twig',
    'single.twig',
];;
Timber::render('base.twig', $context);