cafe_has_field()
Purpose
Checks whether a field has a saved value, so you can skip empty markup.
Syntax
cafe_has_field( string $selector, mixed $object_id = null, ?string $object_type = null ): bool
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
$selector |
string | - | Field name or field key. |
$object_id |
mixed | null |
Which object. null means the current object. |
$object_type |
string|null | null |
Object type, or null to infer it. |
Return value
true when a value is saved for this field and object; otherwise false
(also when the field does not exist).
Example
<?php if ( cafe_has_field( 'hero_title' ) ) : ?>
<h1><?php echo esc_html( cafe_get_field( 'hero_title' ) ); ?></h1>
<?php endif; ?>
Notes
- Default values do not count as saved.
- Costs no extra query after the object's values are loaded.