Utility Functions
These functions are available directly in any expression.
| Function | Description | Example |
|---|---|---|
min | Smallest value | {{ min(1, 2, 3) }} or {{ min(prices) }} |
max | Largest value | {{ max(1, 2, 3) }} or {{ max(prices) }} |
random | Random value | {{ random() }} or {{ random(items) }} |
range | Generate a sequence | {{ range(1, 10) }} or {{ range(0, 100, 5) }} |
cycle | Cycle through values | {{ cycle(['odd', 'even'], loop.index0) }} |
date | Create a date | {{ date() }} or {{ date('now') }} |
html_classes | Conditional CSS classes | {{ html_classes('btn', {active: true}) }} |
min / max
Accept either multiple arguments or an array:
{{ min(10, 5, 20) }} → 5
{{ max(10, 5, 20) }} → 20
{{ min(prices) }} → smallest in array
{{ max(prices) }} → largest in array
random
Returns a random value. Behavior depends on the input type:
{{ random() }} → random integer
{{ random(['red', 'green', 'blue']) }} → random element
{{ random(10) }} → random 0..10
{{ random('abcdef') }} → random character
range
Generates a sequence of values. Supports integers, floats, and characters:
{{ range(1, 5)|join(', ') }} → 1, 2, 3, 4, 5
{{ range(0, 100, 10)|join(', ') }} → 0, 10, 20, ..., 100
{{ range('a', 'z')|join('') }} → abc...z
A sequence longer than 1,000 items gives an empty list, to prevent resource exhaustion.
cycle
Cycles through an array based on an index — useful in loops for alternating values. The index counts from 0, as in Twig, so pass loop.index0:
{{ cycle(['odd', 'even'], loop.index0) }}
{{ cycle(['primary', 'secondary', 'accent'], loop.index0) }}
date
Creates a date in the site's timezone, as Twig's date(): now by default, or from a date string, a timestamp or a date. Format it with the date filter:
{{ date()|date('Y-m-d') }}
{{ date('now')|date('F j, Y') }}
{{ date(post.timestamp)|date('F j, Y') }}
{{ "now"|date('Y') }}
A date string without a timezone reads in UTC, as in Twig and Timber: WordPress sets PHP's timezone to UTC. A second argument converts the date to a timezone; false keeps the date's own.
Compare it with another date, such as a date custom field with transform_value:
{% if post.meta('event_date', {transform_value: true}) >= date() %}
{% if date(post.timestamp) > date('-7 days') %}
html_classes
Generates a space-separated class string. Strings are always included, objects include the key only when the value is truthy:
{{ html_classes('btn', {active: is_active, disabled: is_disabled}) }}
If is_active is true and is_disabled is false, the output is btn active.