I am using Timber with Wordpress (version 5.4.2). I have installed Timber's starter theme as a boilerplate.
Timber leverages the Wordpress template hierarchy allowing you to create a custom PHP file for a given route.
Page.php (default in Timber starter theme)
/**
* The template for displaying all pages.
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages
* and that other 'pages' on your WordPress site will use a
* different template.
*
* To generate specific templates for your pages you can use:
* /mytheme/templates/page-mypage.twig
* (which will still route through this PHP file)
* OR
* /mytheme/page-mypage.php
* **(in which case you'll want to duplicate this file and save to the above path)**
*
* Methods for TimberHelper can be found in the /lib sub-directory
*
* @package WordPress
* @subpackage Timber
* @since Timber 0.1
*/
$context = Timber::context();
$timber_post = new Timber\Post();
$context['post'] = $timber_post;
Timber::render( [ 'page-' . $timber_post->post_name . '.twig', 'page.twig' ], $context );
As per the comments in page.php and the Timber docs, I can create a custom PHP file to load a template for a given page by create it in the root directory of the theme (mytheme/my-custom-php-file.php
)
But I will be creating a lot of custom PHP files for the project I'm working on - it would be pretty messy and hard to manage if I just drop them all into the root directory of the theme.
I would instead like to place these files into their own directory mytheme/src/
. ex. mytheme/src/my-custom-php-file.php
.
Currently, Timber/Wordpress will not recognize this file in this directory.
Where in Timber and/or Wordpress is the directory in which to look for pages' PHP files defined and how can I update this to indicate mytheme/src/
?
index.php
to include your conditions, but it could get messy really fast, unless your pages/posts follow a pattern. For instance, if they are CPTs you can do something likeif(is_singular('news')){/*Logic here*/}
– Chris Haas