Timber Logo

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

Timber\Menu

Overview #

Properties #

NameTypeDescription
depthintegerThe depth of the menu we are rendering
itemsarray/nullArray of Timber\Menu objects you can to iterate through.
idintegerThe ID of the menu, corresponding to the wp_terms table.
IDintegerThe ID of the menu, corresponding to the wp_terms table.
term_idintegerThe ID of the menu, corresponding to the wp_terms table.
namestringThe name of the menu (ex: Main Navigation).
titlestringThe name of the menu (ex: Main Navigation).
optionsarrayAn array of menu options.
raw_optionsarrayThe unfiltered options sent forward via the user in the __construct
theme_locationstringThe theme location of the menu, if available.
taxonomystringthe WordPress taxonomy slug (ex: post_tag or actors)

Methods #

NameTypeReturns/Description
find_parent_item_in_menu\Timber\MenuItem/boolA menu item. False if no parent was found.
get_itemsarrayArray of Timber\MenuItem objects. Empty array if no items could be found.

Class Methods #

__construct #

__construct( int/string $slug, array $options=array() )

returns: void

Initialize a menu.

NameTypeDescription
$slugint/stringA menu slug, the term ID of the menu, the full name from the admin menu, the slug of the registered location or nothing. Passing nothing is good if you only have one menu. Timber will grab what it finds.
$optionsarrayOptional. An array of options. Right now, only the depth is supported which says how many levels of hierarchy should be included in the menu. Default 0, which is all levels.

find_parent_item_in_menu #

find_parent_item_in_menu( array $menu_items, int $parent_id )

returns: \Timber\MenuItem/bool A menu item. False if no parent was found.

Find a parent menu item in a set of menu items.

NameTypeDescription
$menu_itemsarrayAn array of menu items.
$parent_idintThe parent ID to look for.

get_items #

get_items( )

returns: array Array of Timber\MenuItem objects. Empty array if no items could be found.

Get menu items.

Instead of using this function, you can use the $items property directly to get the items for a menu.

Twig

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

This class extends \Timber\Term

This class implements \Timber\CoreInterface