Skip to main content
Timber

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

Methods #

NameReturn TypeSummary/Returns
__call()mixedMagic method dispatcher for meta fields, for convenience in Twig views.

Returns: The value of the meta field named $field if truthy, false otherwise.
__construct()Initialize a menu.
can_edit()boolChecks whether the current user can edit the menu.
convert()Finds any WP_Post objects and converts them to Timber\Posts
fetch_meta()mixedGets an object meta value.

Returns: The custom field value or an array of custom field values. Null if no value could be found.
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.
meta()mixedGets an object meta value.

Returns: The custom field value or an array of custom field values. Null if no value could be found.
raw_meta()null or mixedGets an object meta value directly from the database.

Returns: The meta field value(s). Null if no value could be found, an empty array if all fields were requested but no values could be found.

Class Methods #

__call() #

Magic method dispatcher for meta fields, for convenience in Twig views.

Called when explicitly invoking non-existent methods on a Core object. This method is not meant to be called directly.

link https://secure.php.net/manual/en/language.oop5.overloading.php#object.call

__call( string $field, array $arguments )

Returns: mixed The value of the meta field named $field if truthy, false otherwise.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$fieldstringThe name of the method being called.
$argumentsarrayEnumerated array containing the parameters passed to the function. Not used.

PHP

$post = Timber\Post::get( get_the_ID() );

update_post_meta( $post->id, 'favorite_zep4_track', 'Black Dog' );

Timber::render( 'rock-n-roll.twig', array( 'post' => $post ) );

Twig

{# Since this method does not exist explicitly on the Post class,
it will dynamically dispatch the magic __call() method with an argument
of "favorite_zep4_track" #}

<span>Favorite <i>Zeppelin IV</i> Track: {{ post.favorite_zep4_track() }}</span>

__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.

can_edit() #

Checks whether the current user can edit the menu.

since 2.0.0

Returns: bool


convert() #

Finds any WP_Post objects and converts them to Timber\Posts

convert( array|\Timber\CoreEntity $data )

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$dataarray or \Timber\CoreEntity

fetch_meta() #

Gets an object meta value.

Returns a meta value or all meta values for all custom fields of an object saved in the object meta database table.

Fetching all values is only advised during development, because it can have a big performance impact, when all filters are applied.

fetch_meta( string $field_name = '', array $args = [], bool $apply_filters = true )

Returns: mixed The custom field value or an array of custom field values. Null if no value could be found.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$field_namestringOptional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string.
$argsarrayAn array of arguments for getting the meta value. Third-party integrations can use this argument to make their API arguments available in Timber. Default empty array.

$apply_filtersboolWhether to apply filtering of meta values. You can also use the raw_meta() method as a shortcut to apply this argument. Default true.

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 %}

meta() #

Gets an object meta value.

Returns a meta value or all meta values for all custom fields of an object saved in the meta database table.

Fetching all values is only advised during development, because it can have a big performance impact, when all filters are applied.

meta( string $field_name = '', array $args = [] )

Returns: mixed The custom field value or an array of custom field values. Null if no value could be found.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$field_namestringOptional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string.
$argsarrayAn array of arguments for getting the meta value. Third-party integrations can use this argument to make their API arguments available in Timber. Default empty array.

raw_meta() #

Gets an object meta value directly from the database.

Returns a raw meta value or all raw meta values saved in the meta database table. In comparison to meta(), this function will return raw values that are not filtered by third- party plugins.

Fetching raw values for all custom fields will not have a big performance impact, because WordPress gets all meta values, when the first meta value is accessed.

since 2.0.0

raw_meta( string $field_name = '' )

Returns: null|mixed The meta field value(s). Null if no value could be found, an empty array if all fields were requested but no values could be found.

This method is inherited from \Timber\CoreEntity.

NameTypeDescription
$field_namestringOptional. The field name for which you want to get the value. If no field name is provided, this function will fetch values for all custom fields. Default empty string.