Timber Logo

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

Timber\MenuItem

Overview #

Properties #

NameTypeDescription
childrenarrayArray of children of a menu item. Empty if there are no child menu items.
has_child_classboolWhether the menu item has a menu-item-has-children CSS class.
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.

Methods #

NameTypeReturns/Description
__toStringstringThe label for the menu item.
childrenarray/boolArray of children of a menu item. Empty if there are no child menu items.
is_externalboolWhether the link is external or not.
linkstringA full URL, like http://mysite.com/thing/.
master_objectmixedWhatever object (Timber\Post, Timber\Term, etc.) the menu item represents.
menu\Timber\MenuThe Timber\Menu object the menu item is associated with.
metamixedWhatever value is stored in the database.
namestringThe label for the menu item.
pathstringThe path of a URL, like /foo.
slugstringThe URL-safe slug of the menu item.
thumbnail\Timber\Image/nullThe featured image object.
titlestringThe public label, like "Foo".
typestringThe type of the menu item.

Class Methods #

__toString #

__toString( )

returns: string The label for the menu item.

Magic method to get the label for the menu item.

Twig

<a href="{{ item.link }}">{{ item }}</a>

add_child #

add_child( \Timber\MenuItem $item )

returns: void

Add a new Timber\MenuItem object as a child of this menu item.

NameTypeDescription
$item\Timber\MenuItemThe menu item to add.

add_class #

add_class( string $class_name )

returns: void

Add a CSS class the menu item should have.

NameTypeDescription
$class_namestringCSS class name to be added.

children #

children( )

returns: array/bool Array of children of a menu item. Empty if there are no child menu items.

Get the child menu items of a Timber\MenuItem.

Twig

{% for child in item.children %}
<li class="nav-drop-item">
<a href="{{ child.link }}">{{ child.title }}</a>
</li>
{% endfor %}

is_external #

is_external( )

returns: bool Whether the link is external or not.

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 create rules for the target of a link.

Twig

<a href="{{ item.link }}" target="{{ item.is_external ? '_blank' : '_self' }}">

link( )

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

Get the full link to a menu item.

Twig

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

master_object #

master_object( )

returns: mixed Whatever object (Timber\Post, Timber\Term, etc.) the menu item represents.

Allows dev to access the "master object" (ex: post or page) 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( )

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

Timber Menu.


meta #

meta( string $key )

returns: mixed Whatever value is stored in the database.

Get a meta value of the menu item.

Plugins like Advanced Custom Fields allow you to set custom fields for menu items. With this method you can retrieve the value of these.

NameTypeDescription
$keystringThe meta key to get the value for.

Twig

<a class="icon-{{ item.meta('icon') }}" href="{{ item.link }}">{{ item.title }}</a>

name #

name( )

returns: string The label for the menu item.

Get the label for the menu item.


path #

path( )

returns: string The path of a URL, like /foo.

Get the relative path of the menu item’s link.

Twig

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

slug #

slug( )

returns: string The URL-safe slug of the menu item.

Get the slug for the menu item.

Twig

<ul>
{% for item in menu.items %}
<li class="{{ item.slug }}">
<a href="{{ item.link }}">{{ item.name }}</a>
</li>
{% endfor %}
</ul>

thumbnail #

DEPRECATED since 1.5.2 to be removed in v2.0

thumbnail( )

returns: \Timber\Image/null The featured image object.

Get the featured image of the post associated with the menu item.

Twig

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

title #

title( )

returns: string The public label, like "Foo".

Get the public label for the menu item.

Twig

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

type #

type( )

returns: string The type of the menu item.

Get the type of the menu item.

Depending on what is the menu item links to. Can be post_type for posts, pages and custom posts, post_type_archive for post type archive pages, taxonomy for terms or custom for custom links.


This class extends \Timber\Core

This class implements \Timber\CoreInterface