Timber\Menu
Overview #
This class extends Timber\CoreEntity
Properties #
Name | Type | Description |
---|---|---|
$depth | int | The depth of the menu we are rendering |
$items | array or null | Array of Timber\Menu objects you can to iterate through. |
$id | int | The ID of the menu, corresponding to the wp_terms table. |
$ID | int | The ID of the menu, corresponding to the wp_terms table. |
$term_id | int | The ID of the menu, corresponding to the wp_terms table. |
$name | string | The name of the menu (ex: Main Navigation ). |
$slug | string | The menu slug. |
$title | string | The name of the menu (ex: Main Navigation ). |
$args | object | An object of menu args. |
$raw_args | array | The unfiltered args sent forward via the user in the __construct |
$theme_location | string | The theme location of the menu, if available. |
Methods #
Name | Return Type | Summary/Returns |
---|---|---|
__construct() | Initialize a menu. | |
can_edit() | bool | Checks whether the current user can edit the menu. |
find_parent_item_in_menu() | \Timber\MenuItem or null | Find a parent menu item in a set of menu items. Returns: A menu item. False if no parent was found. |
mixed | Gets a menu meta value. Returns: The meta field value. | |
get_items() | array | Get 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 = [] )
Name | Type | Description |
---|---|---|
$term | \WP_Term or null | A 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. |
$args | array | Optional. 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.
Name | Type | Description |
---|---|---|
$menu_items | array | An array of menu items. |
$parent_id | int | The parent ID to look for. |
get_field() #
Gets a menu meta value.
DEPRECATED since 2.0.0, use {{ menu.meta('field_name') }}
instead.
get_field( string $field_name = null )
Returns: mixed
The meta field value.
Name | Type | Description |
---|---|---|
$field_name | string | The 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