dozabg1616 2013-11-22 21:28
浏览 47
已采纳

如何在Drupal 7中显示数组的所有值

I'm working on a Drupal 7 site with an event listing. I've added a date field so the user can specify the date or dates for the event. Now I'm trying to get the date(s) to show in the template. I've tried using this:

<?php print $node->field_event_date['und'][0]['value']; ?>

That works ok, but it only shows one event from the array. I could just repeat that line 10 times and replace the array item number in each one, but I figure there has to be a way to show all the items in an array, whether it's one or ten. Can this be done with PHP or do I need to make a View?

  • 写回答

1条回答 默认 最新

  • donglu953744 2013-11-22 22:13
    关注

    In PHP terms you're probably looking for a foreach loop, to iterate over the array and print the values.

    In Drupal terms, you'll want to use that with the field_get_items() function:

    $items = field_get_items('node', $node, 'field_event_date');
    foreach ($items as $item) {
      print $item['value'];
    }
    

    For bonus fun, check out EntityMetadataWrappers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?