Timber\MenuItem
Overview #
This class extends Timber\CoreEntity
This class implements Stringable
Properties #
Name | Type | Description |
---|---|---|
$children | array | Array of children of a menu item. Empty if there are no child menu items. |
$classes | array | Array of class names. |
$current | bool | Whether the menu item links to the currently displayed page. |
$current_item_ancestor | bool | Whether the menu item refers to an ancestor (including direct parent) of the currently displayed page. |
$current_item_parent | bool | Whether the menu item refers to the parent item of the currently displayed page. |
$object | string | The underlying menu object type. E.g. a post type name, a taxonomy name or 'custom'. |
$object_id | int or null | Linked object ID. |
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 | Magic method to get the label for the menu item. Returns: The label for the menu item. |
add_child() | Add a new Timber\MenuItem object as a child of this menu item. | |
can_edit() | bool | Checks whether the current user can edit the menu item. |
children() | array or bool | Get the child menu items of a Timber\MenuItem .Returns: Array of children of a menu item. Empty if there are no child menu items. |
convert() | Finds any WP_Post objects and converts them to Timber\Posts | |
bool | Checks to see if the menu item is an external link. Returns: Whether the link is external or not. | |
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. |
mixed | Gets a menu item meta value. Returns: The meta field value. | |
is_external() | bool | Checks to see if the menu item is an external link. Returns: Whether the link is external or not. |
link() | string | Get the full link to a menu item. Returns: A full URL, like https://mysite.com/thing/ . |
master_object() | mixed or null | Allows dev to access the "master object" (ex: post, page, category, post type object) the menu item represents Returns: Whatever object (Timber\Post, Timber\Term, etc.) the menu item represents. |
menu() | \Timber\Menu | Timber Menu. Returns: The Menu object the menu item is associated with. |
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. |
name() | string | Get the label for the menu item. Returns: The label for the menu item. |
path() | string | Get the relative path of the menu item’s link. Returns: The path of a URL, like /foo . |
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. |
slug() | string | Get the slug for the menu item. Returns: The URL-safe slug of the menu item. |
title() | string | Get the public label for the menu item. Returns: The public label, like "Foo". |
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() #
Magic method to get the label for the menu item.
Returns: string
The label for the menu item.
Twig
<a href="{{ item.link }}">{{ item }}</a>
add_child() #
Add a new Timber\MenuItem
object as a child of this menu item.
add_child( \Timber\MenuItem $item )
Name | Type | Description |
---|---|---|
$item | \Timber\MenuItem | The menu item to add. |
can_edit() #
Checks whether the current user can edit the menu item.
since 2.0.0
Returns: bool
children() #
Get the child menu items of a Timber\MenuItem
.
Returns: array|bool
Array of children of a menu item. Empty if there are no child menu items.
Twig
{% for child in item.children %}
<li class="nav-drop-item">
<a href="{{ child.link }}">{{ child.title }}</a>
</li>
{% endfor %}
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 |
external() #
Checks to see if the menu item is an external link.
DEPRECATED since 2.0.0, use {{ item.is_external }}
see Timber\MenuItem::is_external()
Returns: bool
Whether the link is external or not.
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_field() #
Gets a menu item meta value.
DEPRECATED since 2.0.0, use {{ item.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. |
is_external() #
Checks to see if the menu item is an external link.
If your site is example.org
, then google.com/whatever
is an external link. This is helpful when you want to style external links differently or create rules for the target of a link.
Returns: bool
Whether the link is external or not.
Twig
<a href="{{ item.link }}" target="{{ item.is_external ? '_blank' : '_self' }}">
Or when you only want to add a target attribute if it is really needed:
Twig
<a href="{{ item.link }}" {{ item.is_external ? 'target="_blank"' }}>
In combination with is_target_blank()
:
Twig
<a href="{{ item.link }}" {{ item.is_external or item.is_target_blank ? 'target="_blank"' }}>
link() #
Get the full link to a menu item.
Returns: string
A full URL, like https://mysite.com/thing/
.
Twig
{% for item in menu.items %}
<li><a href="{{ item.link }}">{{ item.title }}</a></li>
{% endfor %}
master_object() #
Allows dev to access the "master object" (ex: post, page, category, post type object) the menu item represents
Returns: mixed|null
Whatever object (Timber\Post, Timber\Term, etc.) the menu item represents.
Twig
<div>
{% for item in menu.items %}
<a href="{{ item.link }}"><img src="{{ item.master_object.thumbnail }}" /></a>
{% endfor %}
</div>
menu() #
Timber Menu.
since 1.12.0
Returns: \Timber\Menu
The Menu
object the menu item is associated with.
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. |
name() #
Get the label for the menu item.
Returns: string
The label for the menu item.
path() #
Get the relative path of the menu item’s link.
Returns: string
The path of a URL, like /foo
.
Twig
{% for item in menu.items %}
<li><a href="{{ item.path }}">{{ item.title }}</a></li>
{% endfor %}
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. |
slug() #
Get the slug for the menu item.
Returns: string
The URL-safe slug of the menu item.
Twig
<ul>
{% for item in menu.items %}
<li class="{{ item.slug }}">
<a href="{{ item.link }}">{{ item.name }}</a>
</li>
{% endfor %}
</ul>
title() #
Get the public label for the menu item.
Returns: string
The public label, like "Foo".
Twig
{% for item in menu.items %}
<li><a href="{{ item.link }}">{{ item.title }}</a></li>
{% endfor %}