I would like to combine several fields (ie metadata) I created with texts in the show.php page.
The goal is to obtain, for example: “Birth: 1805 in New-York” where 1805 and “New-York” are two different metadata.
I achieved this goal with the following code.
<?php if ($meta = metadata('item', array('Item Type Metadata', 'Date of Birth'))): ?>
<p>Birth:
<?php echo $meta.' in '; ?>
<?php if ($meta = metadata('item', array('Item Type Metadata', 'Place of Birth'))): ?>
<?php echo ($meta).''; ?>
</p>
<?php endif; ?>
<?php endif; ?>
But it is not very efficient and suppose to open a lot of if. I have a lot of fields (~30) and it is not very convenient. Any idea to simply this code? I rather prefer to have something like:
A single if where $birthdate and $birthplace are verified as no null. If both fields have contents, then display:
Birth: $birthdate in $birthplace
When you pass in the all option to metadata, it gives you an array of all the values. Thus, you’ll need to loop through them for the display. Maybe something like
<?php $citizenships= metadata('item', array('Item Type Metadata', 'Citizenship'), array('all' => true); //note the plural to indicate an array
if (!empty($citizenships):
?>
<h3>Citizenship</h3>
<?php foreach ($citizenships and $citizenship): ?>
<?php echo $citizenship; ?>
<?php endforeach; >
?>
There are a couple easy ways to deal with multiple values.
One is built directly into metadata(): the delimiter option.
echo metadata('item', array('Item Type Metadata', 'Citizenship'), array('delimiter' => ', '));
When there are multiple values for a field, the string given as the delimiter (a comma and a space in my example) is used to combine the values. So you’d get output like Value 1, Value 2, Value 3 rather than just Value 1 if there are 3 values.
If you want to do something more complex, Patrick’s suggestion of the all option is the way to go. There are just some small syntax problems with his example.