Skip to main content
Timber

You are reading the documentation for Timber v2.x. Switch to the documentation for Timber v1.x.

Timber\​Theme

Need to display info about your theme? Well you've come to the right place. By default info on the current theme comes for free with what's fetched by Timber::context() in which case you can access it your theme like so:

PHP

<?php
$context = Timber::context();

Timber::render('index.twig', $context);
?>

Twig

<script src="{{ theme.link }}/static/js/all.js"></script>

HTML

<script src="https://example.org/wp-content/themes/my-theme/static/js/all.js"></script>

Overview #

This class extends Timber\Core
This class implements JsonSerializable

Properties #

NameTypeDescription
$namestringthe human-friendly name of the theme (ex: My Timber Starter Theme)
$parent\Timber\Themethe Timber\Theme object for the parent theme
$parent_slugstringthe slug of the parent theme (ex: _s)
$slugstringthe slug of the theme (ex: my-timber-theme)
$uristringRetrieves template directory URI for the active (parent) theme. (ex: https://example.org/wp-content/themes/my-timber-theme).
$versionstringthe version of the theme (ex: 1.2.3)

Methods #

NameReturn TypeSummary/Returns
__call()mixedMagic method dispatcher for meta fields, for convenience in Twig views.

Returns: The value of the meta field named $field if truthy, false otherwise.
__construct()Constructs a new Timber\Theme object.
display()false or stringGets a theme header, formatted and translated for display.
get()false or stringGets a raw, unformatted theme header.

Returns: String on success, false on failure.
link()string

Returns: Retrieves template directory URI for the active (child) theme. (ex: https://example.org/wp-content/themes/my-timber-theme).
path()string

Returns: The relative path to the theme (ex: /wp-content/themes/my-timber-theme).
theme_mod()string
theme_mods()array

Class Methods #

__call() #

Magic method dispatcher for meta fields, for convenience in Twig views.

Called when explicitly invoking non-existent methods on a Core object. This method is not meant to be called directly.

link https://secure.php.net/manual/en/language.oop5.overloading.php#object.call

__call( string $field, array $arguments )

Returns: mixed The value of the meta field named $field if truthy, false otherwise.

This method is inherited from \Timber\Core.

NameTypeDescription
$fieldstringThe name of the method being called.
$argumentsarrayEnumerated array containing the parameters passed to the function. Not used.

PHP

$post = Timber\Post::get( get_the_ID() );

update_post_meta( $post->id, 'favorite_zep4_track', 'Black Dog' );

Timber::render( 'rock-n-roll.twig', array( 'post' => $post ) );

Twig

{# Since this method does not exist explicitly on the Post class,
it will dynamically dispatch the magic __call() method with an argument
of "favorite_zep4_track" #}

<span>Favorite <i>Zeppelin IV</i> Track: {{ post.favorite_zep4_track() }}</span>

__construct() #

Constructs a new Timber\Theme object.

The Timber\Theme object of the current theme comes in the default Timber::context() call. You can access this in your twig template via {{ site.theme }}.

__construct( string $slug = null )

NameTypeDescription
$slugstring

PHP

<?php
$theme = new Timber\Theme("my-timber-theme");
$context['theme_stuff'] = $theme;
Timber::render('single.twig', $context);

Twig

We are currently using the {{ theme_stuff.name }} theme.

HTML

We are currently using the My Theme theme.

display() #

Gets a theme header, formatted and translated for display.

see WP_Theme::display()

display( string $header )

Returns: false|string

NameTypeDescription
$headerstringName of the theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.

Twig

{{ theme.display('Description') }}

get() #

Gets a raw, unformatted theme header.

see WP_Theme::get()

get( string $header )

Returns: false|string String on success, false on failure.

NameTypeDescription
$headerstringName of the theme header. Name, Description, Author, Version, ThemeURI, AuthorURI, Status, Tags.

Twig

{{ theme.get('Version') }}

Returns: string Retrieves template directory URI for the active (child) theme. (ex: https://example.org/wp-content/themes/my-timber-theme).


path() #

Returns: string The relative path to the theme (ex: /wp-content/themes/my-timber-theme).


theme_mod() #

theme_mod( string $name, bool $default = false )

Returns: string

NameTypeDescription
$namestring
$defaultbool

theme_mods() #

Returns: array