Hi,
I’m using the following code to display both the field label and value:
<?php
$field_name = "distance";
$field = get_field_object($field_name);
echo $field['label'] . ': ' . $field['value'];
?>
The output on the front-end of this is Distance: 33miles
How do I wrap a span around the field label so I can style it? <span>Distance:</span> 33miles
Also, how do I get the field label to display ONLY if it has a value?
Hi this is what I have used
if(get_field(‘cultivar’)){
$field_name = “cultivar”;
$field = get_field_object($field_name);
echo ‘‘, $field[‘label’],’‘ . ‘: ‘ . ‘<div class=”headlinename”>’, $field[‘value’],'</div>’,'</br>’;}
The code has a div wrapped round the value so you can use ::after content: ‘miles’; if you liked.
This piece of code only shows when there is a value too.
Thanks but I wanted the label wrapping in HTML not the value, also because you didn’t put your code in code tags when copying/pasting the quotes/apostrophes will be the wrong format.
I found this code which seems to work well:
<?php $field_name = "distance";
$field = get_field_object($field_name);?>
<?php if ($field['value']): ?>
<span class='post-meta-key'><?php echo $field['label']; ?>:</span> <?php echo $field['value']; ?>
<?php endif; ?>
Let me know if this is a good/bad solution.