Skip to main content
Timber

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

Timber\​Term

Terms: WordPress has got 'em, you want 'em. Categories. Tags. Custom Taxonomies. You don't care, you're a fiend. Well let's get this under control:

PHP

// Get a term by its ID
$context['term'] = Timber::get_term(6);

// Get a term when on a term archive page
$context['term_page'] = Timber::get_term();

// Get a term with a slug
$context['team'] = Timber::get_term('patriots');
Timber::render('index.twig', $context);

Twig

<h2>{{ term_page.name }} Archives</h2>
<h3>Teams</h3>
<ul>
<li>{{ st_louis.name}} - {{ st_louis.description }}</li>
<li>{{ team.name}} - {{ team.description }}</li>
</ul>

HTML

<h2>Team Archives</h2>
<h3>Teams</h3>
<ul>
<li>St. Louis Cardinals - Winner of 11 World Series</li>
<li>New England Patriots - Winner of 6 Super Bowls</li>
</ul>

Overview #

This class extends Timber\CoreEntity
This class implements Stringable

Properties #

NameTypeDescription
$namestringthe human-friendly name of the term (ex: French Cuisine)
$taxonomystringthe WordPress taxonomy slug (ex: post_tag or actors)

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.
__toString()stringThe string the term will render as by default
can_edit()boolChecks whether the current user can edit the term.
convert()Finds any WP_Post objects and converts them to Timber\Posts
description()stringReturn the description of the term
edit_link()string or nullGets the edit link for a term if the current user has the correct rights.

Returns: The edit URL of a term in the WordPress admin or null if the current user can’t edit the term.
fetch_meta()mixedGets an object meta value.

Returns: The custom field value or an array of custom field values. Null if no value could be found.
get_children()array
get_field()mixedGets a term meta value.

Returns: The meta field value.
get_posts()array or bool or nullGet Posts that have been "tagged" with the particular term
link()stringReturns a full link to the term archive page like https://example.com/category/news
meta()mixedGets an object meta value.

Returns: The custom field value or an array of custom field values. Null if no value could be found.
path()stringReturns a relative link (path) to the term archive page like /category/news
posts()\Timber\PostQueryGets posts that have the current term assigned.
raw_meta()null or mixedGets an object meta value directly from the database.

Returns: The meta field value(s). Null if no value could be found, an empty array if all fields were requested but no values could be found.
title()string

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\CoreEntity.

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>

__toString() #

The string the term will render as by default

Returns: string


can_edit() #

Checks whether the current user can edit the term.

Returns: bool

Twig

{% if term.can_edit %}
<a href="{{ term.edit_link }}">Edit</a>
{% endif %}

convert() #

Finds any WP_Post objects and converts them to Timber\Posts

convert( array|\Timber\CoreEntity $data )

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$dataarray or \Timber\CoreEntity

description() #

Return the description of the term

Returns: string


Gets the edit link for a term if the current user has the correct rights.

Returns: string|null The edit URL of a term in the WordPress admin or null if the current user can’t edit the term.

Twig

{% if term.can_edit %}
<a href="{{ term.edit_link }}">Edit</a>
{% endif %}

fetch_meta() #

Gets an object meta value.

Returns a meta value or all meta values for all custom fields of an object saved in the object meta database table.

Fetching all values is only advised during development, because it can have a big performance impact, when all filters are applied.

fetch_meta( string $field_name = '', array $args = [], bool $apply_filters = true )

Returns: mixed The custom field value or an array of custom field values. Null if no value could be found.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$field_namestringOptional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string.
$argsarrayAn array of arguments for getting the meta value. Third-party integrations can use this argument to make their API arguments available in Timber. Default empty array.

$apply_filtersboolWhether to apply filtering of meta values. You can also use the raw_meta() method as a shortcut to apply this argument. Default true.

get_children() #

DEPRECATED since 2.0.0, use {{ term.children }} instead.

Returns: array


get_field() #

Gets a term meta value.

DEPRECATED since 2.0.0, use {{ term.meta('field_name') }} instead.

see Timber\Term::meta()

get_field( string $field_name = null )

Returns: mixed The meta field value.

NameTypeDescription
$field_namestringThe field name for which you want to get the value.

get_posts() #

Get Posts that have been "tagged" with the particular term

DEPRECATED since 2.0.0 use {{ term.posts }} instead

get_posts( int $numberposts = 10 )

Returns: array|bool|null

NameTypeDescription
$numberpostsint

Returns a full link to the term archive page like https://example.com/category/news

Returns: string

Twig

See all posts in: <a href="{{ term.link }}">{{ term.name }}</a>

meta() #

Gets an object meta value.

Returns a meta value or all meta values for all custom fields of an object saved in the meta database table.

Fetching all values is only advised during development, because it can have a big performance impact, when all filters are applied.

meta( string $field_name = '', array $args = [] )

Returns: mixed The custom field value or an array of custom field values. Null if no value could be found.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$field_namestringOptional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string.
$argsarrayAn array of arguments for getting the meta value. Third-party integrations can use this argument to make their API arguments available in Timber. Default empty array.

path() #

Returns a relative link (path) to the term archive page like /category/news

Returns: string

Twig

See all posts in: <a href="{{ term.path }}">{{ term.name }}</a>

posts() #

Gets posts that have the current term assigned.

see https://timber.github.io/docs/v2/guides/posts/

posts( int|array $query = [], string $post_type_or_class = null )

Returns: \Timber\PostQuery

NameTypeDescription
$queryint or arrayOptional. Either the number of posts or an array of arguments for the post query to be performed. Default is an empty array, the equivalent of: php [ 'posts_per_page' => get_option('posts_per_page'), 'post_type' => 'any', 'tax_query' => [ ...tax query for this Term... ] ]
$post_type_or_classstringDeprecated. Before Timber 2.x this was a post_type to be used for querying posts OR the Timber\Post subclass to instantiate for each post returned. As of Timber 2.0.0, specify post_type in the $query array argument. To specify the class, use Class Maps.

Query the default posts_per_page for this Term:

Twig

<h4>Recent posts in {{ term.name }}</h4>

<ul>
{% for post in term.posts() %}
<li>
<a href="{{ post.link }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>

Query exactly 3 Posts from this Term:

Twig

<h4>Recent posts in {{ term.name }}</h4>

<ul>
{% for post in term.posts(3) %}
<li>
<a href="{{ post.link }}">{{ post.title }}</a>
</li>
{% endfor %}
</ul>

If you need more control over the query that is going to be performed, you can pass your custom query arguments in the first parameter.

Twig

<h4>Our branches in {{ region.name }}</h4>

<ul>
{% for branch in region.posts({
post_type: 'branch',
posts_per_page: -1,
orderby: 'menu_order'
}) %}

<li>
<a href="{{ branch.link }}">{{ branch.title }}</a>
</li>
{% endfor %}
</ul>

raw_meta() #

Gets an object meta value directly from the database.

Returns a raw meta value or all raw meta values saved in the meta database table. In comparison to meta(), this function will return raw values that are not filtered by third- party plugins.

Fetching raw values for all custom fields will not have a big performance impact, because WordPress gets all meta values, when the first meta value is accessed.

since 2.0.0

raw_meta( string $field_name = '' )

Returns: null|mixed The meta field value(s). Null if no value could be found, an empty array if all fields were requested but no values could be found.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$field_namestringOptional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string.

title() #

Returns: string