Timber Logo

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

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_parentboolWhether the menu item refers to the parent item of the currently displayed page.
$current_item_ancestorboolWhether the menu item refers to an ancestor (including direct parent) of the currently displayed page.
$object_idint or nullLinked object ID.
$objectstringThe underlying menu object type. E.g. a post type name, a taxonomy name or 'custom'.

Methods #

NameReturn TypeSummary/Returns
__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.
external()boolChecks to see if the menu item is an external link.

Returns: Whether the link is external or not.
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 http://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.
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.
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 #

name() #

Get the label for the menu item.

Returns: string The label for the menu item.


__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>

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>

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>

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.

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

Timber Menu.

since 1.12.0

Returns: \Timber\Menu The Menu object the menu item is associated with.


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.

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

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.


Get the full link to a menu item.

Returns: string A full URL, like http://mysite.com/thing/.

Twig

{% for item in menu.items %}
<li><a href="{{ item.link }}">{{ item.title }}</a></li>
{% endfor %}

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

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

can_edit() #

Checks whether the current user can edit the menu item.

since 2.0.0

Returns: bool