Search code examples
wordpresstimbercustom-pages

How to add new custom page in Timber Wordpress


Wordpress version: 6.4.3 "timber/timber": "2.x-dev" Starter theme: upstatement/timber-starter-theme

I understand you require composer to install timber, no longer require plugin but how to new custom page in timber wordpress?

in page attributes, i cannot see the tempalete dropdown to select the template Page attributes

Did i miss out something?

I did create contact.php

<?php
/**
 * The main template file
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query.
 * E.g., it puts together the home page when no home.php file exists
 *
 * Methods for TimberHelper can be found in the /lib sub-directory
 *
 * @package  WordPress
 * @subpackage  Timber
 * @since   Timber 0.1
 */

$context          = Timber::context();
$context['posts'] = Timber::get_posts();
$context['foo']   = 'bar';
$templates        = array( 'contact.twig' );
Timber::render( $templates, $context );

also the contact.twig

{% extends "base.twig" %}

{% block content %}
    {% include "partial/altHero.twig" %}

    <h1>Contact</h1>

{% endblock %}

I am not sure, what else do i missout? and i cannot install the timber plugin as well, error msg when i try to insstall timber plugin **Plugin could not be activated because it triggered a fatal error. **

Your help is most appreciated Cheers


Solution

  • enter image description here

    ok, finally solved it

    you need add comment in your custom php page template name and description

    without it, it will not display

    <?php
    /**
     * Template Name: My Custom Page<<<<
     * Description: A Page Template with a darker design.<<<<
     * The main template file
     * This is the most generic template file in a WordPress theme
     * and one of the two required files for a theme (the other being style.css).
     * It is used to display a page when nothing more specific matches a query.
     * E.g., it puts together the home page when no home.php file exists
     *
     * Methods for TimberHelper can be found in the /lib sub-directory
     *
     * @package  WordPress
     * @subpackage  Timber
     * @since   Timber 0.1
     */
    
    $context          = Timber::context();
    $context['posts'] = Timber::get_posts();
    $context['foo']   = 'bar';
    $templates        = array( 'contact.twig' );
    Timber::render( $templates, $context );