Archived topic
Displaying complex variables in an Element.
3 replies · Started by Andrew on October 25, 2020
I'm using Meta Box to create custom fields for use in Elements - and they work great together - but a lot of the variables are more complex that a simple variable. In my case I'm wanting to retrieve a variable within an array - something like {{custom_field.array_name.variable_name}}
Also - As the field is conditional - I'd like to display the CTA conditionally. eg.
{% if custom_field.group_cta_fields %}
{{custom_field.group_cta_fields.text_cta}}
{% endif %}
Is this possible?
or is it possible to somehow add a hook area conditionally within an Element?
Thanks.
Hi Andrew,
I'm afraid you can't do conditional statements w/ template tags.
However, you should be able to do that with shortcodes.
You can make a shortcode that does that.
Here's how to make a shortcode: https://docs.generatepress.com/article/creating-a-shortcode/
Thanks. What about targeting variables in arrays?
foreach PHP iteration of variables should work within the shortcode.
You can even add conditions within it too.
Example:
...
foreach($array as $items ) {
$custom_field = $items->custom_field_name;
if(!$custom_field){
//do something if $custom_field is empty.
} else if ($custom_field){
//do something if $custom_field has a value.
}
}
...