Timber\MenuItem
Overview #
This class extends Timber\CoreEntity
Properties #
Name | Type | Description |
---|---|---|
$children | array | Array of children of a menu item. Empty if there are no child menu items. |
$classes | array | Array of class names. |
$current | bool | Whether the menu item links to the currently displayed page. |
$current_item_parent | bool | Whether the menu item refers to the parent item of the currently displayed page. |
$current_item_ancestor | bool | Whether the menu item refers to an ancestor (including direct parent) of the currently displayed page. |
$object_id | int or null | Linked object ID. |
$object | string | The underlying menu object type. E.g. a post type name, a taxonomy name or 'custom'. |
Methods #
Name | Return Type | Summary/Returns |
---|---|---|
__toString() | string | Magic 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() | bool | Checks whether the current user can edit the menu item. |
children() | array or bool | Get the child menu items of a Timber\MenuItem .Returns: Array of children of a menu item. Empty if there are no child menu items. |
bool | Checks to see if the menu item is an external link. Returns: Whether the link is external or not. | |
mixed | Gets a menu item meta value. Returns: The meta field value. | |
is_external() | bool | Checks to see if the menu item is an external link. Returns: Whether the link is external or not. |
link() | string | Get the full link to a menu item. Returns: A full URL, like http://mysite.com/thing/ . |
master_object() | mixed or null | Allows 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\Menu | Timber Menu. Returns: The Timber\Menu object the menu item is associated with. |
name() | string | Get the label for the menu item. Returns: The label for the menu item. |
path() | string | Get the relative path of the menu item’s link. Returns: The path of a URL, like /foo . |
slug() | string | Get the slug for the menu item. Returns: The URL-safe slug of the menu item. |
title() | string | Get 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.
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 )
Name | Type | Description |
---|---|---|
$item | \Timber\MenuItem | The 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"' }}>
menu() #
Timber Menu.
since 1.12.0
Returns: \Timber\Menu
The Timber\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.
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. |
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.
link() #
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