Skip to main content
Timber

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

Timber\​MenuItem

Overview #

This class extends Timber\CoreEntity
This class implements Stringable

Properties #

NameTypeDescription
$childrenarrayArray of children of a menu item. Empty if there are no child menu items.
$classesarrayArray of class names.
$currentboolWhether the menu item links to the currently displayed page.
$current_item_ancestorboolWhether the menu item refers to an ancestor (including direct parent) of the currently displayed page.
$current_item_parentboolWhether the menu item refers to the parent item of the currently displayed page.
$objectstringThe underlying menu object type. E.g. a post type name, a taxonomy name or 'custom'.
$object_idint or nullLinked object ID.

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()stringMagic 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()boolChecks whether the current user can edit the menu item.
children()array or boolGet 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
external()boolChecks to see if the menu item is an external link.

Returns: Whether the link is external or not.
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_field()mixedGets a menu item meta value.

Returns: The meta field value.
is_external()boolChecks to see if the menu item is an external link.

Returns: Whether the link is external or not.
link()stringGet the full link to a menu item.

Returns: A full URL, like https://mysite.com/thing/.
master_object()mixed or nullAllows 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\MenuTimber Menu.

Returns: The Menu object the menu item is associated with.
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.
name()stringGet the label for the menu item.

Returns: The label for the menu item.
path()stringGet the relative path of the menu item’s link.

Returns: The path of a URL, like /foo.
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.
slug()stringGet the slug for the menu item.

Returns: The URL-safe slug of the menu item.
title()stringGet 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.

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() #

Magic method to get the label for the menu item.

see Timber\MenuItem::name()

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 )

NameTypeDescription
$item\Timber\MenuItemThe 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.

NameTypeDescription
$dataarray 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.

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_field() #

Gets a menu item meta value.

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

see Timber\MenuItem::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.

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"' }}>

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>

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.

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.

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.

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.

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 %}