Toolbox Twig functions

To speed up your workflow we have added a few functions to Toolbox. Some are meant for debugging and some are used to query fields more efficiently.

toolbox_get_fields( fields , [post_id] , [options] )

This Twig function gets the field or fields and returns them as an array

Parameters

  • fields(mixed) (required) fieldname (string) or fieldnames (array) that need to be returned
  • post_id (mixed) (optional) post_id where the value is saved. Defaults to current post
  • options (array) (optional) options for Meta Box

Usage

Get a value from the current post
  {% set fields = toolbox_get_fields(['name','address','city']) %}
<h4>{{fields.name}}</h4>
<p>{{fields.address}}, {{fields.city}}</p>
Get a value from a specific post
  {% set fields = toolbox_get_fields(['name','address','city'],123) %}
<h4>{{fields.name}}</h4>
<p>{{fields.address}}, {{fields.city}}</p>
Get a value from a user table (Meta Box only)

This examples shows how to get the values for post id 123, but from the custom table added with the mb-custom-table extension.

  {% set fields = toolbox_get_fields(['name','address','city'],123,{'storage_type':'custom_table', 'table': 'custom_user_table'}) %}
<h4>{{fields.name}}</h4>
<p>{{fields.address}}, {{fields.city}}</p>

toolbox_get_field_objects( fields , [post_id] , [options] )

This Twig function returns the field_objects for the requested field(s). The field_object contain all essential data provided by the custom field solution and can be used to completely rebuild the output of the field.

A good example would be a checkbox field. The function toolbox_get_fields() will only return the selected options, without providing us with a way to list all options that are available on the backend.

The toolbox_get_field_objects() function however queries the field_object and provides you all data used to render both frontend and backend options.

Parameters

  • fields(mixed) (required) fieldname (string) or fieldnames (array) that need to be returned
  • post_id (mixed) (optional) post_id where the value is saved. Defaults to current post
  • options (array) (optional) options for Meta Box

Usage

Get a field_object from the current post
  {% set fields = toolbox_get_field_objects(['my_checkboxes']) %}
<h4>Selected value:</h4>
<p>{{fields.my_checkboxes.value|join(', ')}}</p>
<h4>All available options</h4>
<p>{{fields.my_checkboxes.choices|join( ', ' )}}</p>

toolbox_dump( var )

This Twig function dumps the variable into your layout. It’s useful for debugging your twig values or finding the values on a fieldtype.

Parameters

  • var(mixed) (required) variable that you want to display

Usage

Output the current post variable
  {{toolbox_dump( post )}}

toolbox_get_posts( options )

This is a Twig wrapper function for the WP get_posts() function. It can be used to query WP, using an object with key->value settings.

Parameters

  • options(object) (required) Query Args for getting the posts

Returns

The Twig function returns a WP Posts object.

Usage

  {% set new_posts = toolbox_get_posts({'post_type': 'post', 'cat': 'plugin', 'posts_per_page':5 }) %}
{% for item in new_posts %}
<h4>{{item.title}}</h4>
{% endfor %}

Notice:

A WP Posts object is not the same as a Timber Posts object. By default it doesn’t have all the features. You can still get all information on individual posts though, by resetting the item variable in the loop, this time using Post( item.ID ):

  {% set new_posts = toolbox_get_posts({'post_type': 'post', 'cat': 'plugin', 'posts_per_page':5 }) %}
{% for item in new_posts %}
{% set item = Post(item.ID) %}
<h4>{{item.title}}</h4>
<p>{{item.terms|join(', ')}}</p>
{% endfor %}