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 |
---|---|---|
__toString() | string | The string the term will render as by default |
can_edit() | bool | Checks whether the current user can edit the term. |
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. |
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 |
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. |
title() | string |
Class Methods #
__toString() #
The string the term will render as by default
Returns: string
description() #
Return the description of the term
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 %}
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 %}
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>
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. |
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>
title() #
Returns: string
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 |
get_children() #
DEPRECATED since 2.0.0, use {{ term.children }}
instead.
Returns: array