Timber\Menu
Overview #
This class extends Timber\CoreEntity
This class implements Stringable
Properties #
Name | Type | Description |
---|---|---|
$ID | int | The ID of the menu, corresponding to the wp_terms table. |
$args | object | An object of menu args. |
$depth | int | The depth of the menu we are rendering |
$id | int | The ID of the menu, corresponding to the wp_terms table. |
$items | array or null | Array of Timber\Menu objects you can to iterate through. |
$name | string | The name of the menu (ex: Main Navigation ). |
$raw_args | array | The unfiltered args sent forward via the user in the __construct |
$slug | string | The menu slug. |
$term_id | int | The ID of the menu, corresponding to the wp_terms table. |
$theme_location | string | The theme location of the menu, if available. |
$title | string | The name of the menu (ex: Main Navigation ). |
Methods #
Name | Return Type | Summary/Returns |
---|---|---|
__call() | mixed | Magic 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() | bool | Checks whether the current user can edit the menu. |
convert() | Finds any WP_Post objects and converts them to Timber\Posts | |
fetch_meta() | mixed | Gets 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 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. |
meta() | mixed | Gets 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 mixed | Gets 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
.
Name | Type | Description |
---|---|---|
$field | string | The name of the method being called. |
$arguments | array | Enumerated 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 = [] )
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. |
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
.
Name | Type | Description |
---|---|---|
$data | array 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
.
Name | Type | Description |
---|---|---|
$field_name | string | Optional. 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. |
$args | array | An 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_filters | bool | Whether 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.
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 %}
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
.
Name | Type | Description |
---|---|---|
$field_name | string | Optional. 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. |
$args | array | An 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
.
Name | Type | Description |
---|---|---|
$field_name | string | Optional. 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. |