Timber Logo

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

Timber\​Menu

Overview #

This class extends Timber\CoreEntity
This class implements Stringable

Properties #

NameTypeDescription
$depthintThe depth of the menu we are rendering
$itemsarray or nullArray of Timber\Menu objects you can to iterate through.
$idintThe ID of the menu, corresponding to the wp_terms table.
$IDintThe ID of the menu, corresponding to the wp_terms table.
$term_idintThe ID of the menu, corresponding to the wp_terms table.
$namestringThe name of the menu (ex: Main Navigation).
$slugstringThe menu slug.
$titlestringThe name of the menu (ex: Main Navigation).
$argsobjectAn object of menu args.
$raw_argsarrayThe unfiltered args sent forward via the user in the __construct
$theme_locationstringThe theme location of the menu, if available.

Methods #

NameReturn TypeSummary/Returns
__construct()Initialize a menu.
can_edit()boolChecks whether the current user can edit the menu.
find_parent_item_in_menu()\Timber\MenuItem or nullFind a parent menu item in a set of menu items.

Returns: A menu item. False if no parent was found.
get_field()mixedGets a menu meta value.

Returns: The meta field value.
get_items()arrayGet menu items.

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

Class Methods #

__construct() #

Initialize a menu.

__construct( ?\WP_Term $term, array $args = [] )

NameTypeDescription
$term\WP_Term or nullA 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.
$argsarrayOptional. 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 a parent menu item in a set of menu items.

find_parent_item_in_menu( array $menu_items, int $parent_id )

Returns: \Timber\MenuItem|null A menu item. False if no parent was found.

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

get_field() #

Gets a menu meta value.

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

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

get_items() #

Get menu items.

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

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

Twig

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

can_edit() #

Checks whether the current user can edit the menu.

since 2.0.0

Returns: bool