Combine several item type metadata

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

You could reorder things a little to make it clearer and perhaps smaller:

<?php
$birth_date = metadata('item', array('Item Type Metadata', 'Date of Birth'));
$birth_place = metadata('item', array('Item Type Metadata', 'Place of Birth'));
if ($birth_date && $birth_place):
?>
<p>
Birth: <?php echo $birth_date; ?> in <?php echo $birth_place; ?>
</p>
<?php endif; ?>

Thanks! It is indeed clearer.

One problem occurs when I have two entries for the same field. Let’s say one person has two citizenships. If I use

$citizenship= metadata('item', array('Item Type Metadata', 'Citizenship'));

It will only print the first entry not the second.

You can get all the records by passing in the all option:

$citizenships= metadata('item', array('Item Type Metadata', 'Citizenship'), array('all' => true);

That will give you an array of all the entries.

This prints only “Array” instead of the citizenships for me.

Here is my code:

<?php
$citizenship = metadata('item', array('Item Type Metadata', 'Citizenship'), array('all' => true));
if ($citizenship):
?>
  <h3>Citizenship</h3>
  <?php echo $citizenship; ?>
<?php endif; ?>

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; >
?>

Sorry I don’t understand and cannot get it working.

In your example, $citizenship (singular) is not defined, no?

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.

1 Like

Sorry for the late answer. It works perfectly ! Thanks !