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 #
Name | Type | Description |
---|---|---|
$name | string | the human-friendly name of the term (ex: French Cuisine) |
$taxonomy | string | the WordPress taxonomy slug (ex: post_tag or actors ) |
Methods #
Name | Return Type | Summary/Returns |
---|---|---|
__call() | mixed | Magic method dispatcher for meta fields, for convenience in Twig views. Returns: The value of the meta field named $field if truthy, false otherwise. |
__toString() | string | The string the term will render as by default |
can_edit() | bool | Checks whether the current user can edit the term. |
convert() | Finds any WP_Post objects and converts them to Timber\Posts | |
description() | string | Return the description of the term |
edit_link() | string or null | Gets 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() | mixed | Gets an object meta value. Returns: The custom field value or an array of custom field values. Null if no value could be found. |
array | ||
mixed | Gets a term meta value. Returns: The meta field value. | |
array or bool or null | Get Posts that have been "tagged" with the particular term | |
link() | string | Returns a full link to the term archive page like https://example.com/category/news |
meta() | mixed | Gets an object meta value. Returns: The custom field value or an array of custom field values. Null if no value could be found. |
path() | string | Returns a relative link (path) to the term archive page like /category/news |
posts() | \Timber\PostQuery | Gets posts that have the current term assigned. |
raw_meta() | null or mixed | Gets 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
.
Name | Type | Description |
---|---|---|
$field | string | The name of the method being called. |
$arguments | array | Enumerated 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
.
Name | Type | Description |
---|---|---|
$data | array or \Timber\CoreEntity |
description() #
Return the description of the term
Returns: string
edit_link() #
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
.
Name | Type | Description |
---|---|---|
$field_name | string | Optional. 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. |
$args | array | An 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_filters | bool | Whether 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.
get_field( string $field_name = null )
Returns: mixed
The meta field value.
Name | Type | Description |
---|---|---|
$field_name | string | The 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
Name | Type | Description |
---|---|---|
$numberposts | int |
link() #
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
.
Name | Type | Description |
---|---|---|
$field_name | string | Optional. 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. |
$args | array | An 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
Name | Type | Description |
---|---|---|
$query | int or array | Optional. 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_class | string | Deprecated. 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
.
Name | Type | Description |
---|---|---|
$field_name | string | Optional. 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