0

I have a link field with multiple values. I want to display the link title for each of these fields. I can print the slide link titles individually but not using the foreach.

Currently I have:

<ul id="slideLinks">
<?php $slideshow_counter = 0; 
    $slide_link = $node->field_slideshow_link;
    print $slide_link['und'][0]['title'];
?>
<?php foreach ($slide_link as $link) : ?>
    <li><a class="slideControl" href="javascript:void(0);"><?php print $link['und'][$slideshow_counter]['title']; ?></a></li>
    <?php print $slideshow_counter; ?>
    <?php $slideshow_counter++; ?>
<?php
if ($slideshow_counter == 4)
    break;
endforeach;
?>

1
  • Can you be more clear? What you see in debugger in field_slideshow_link? As I understand $slide_link['und'] is an array that contains all field values?
    – mixerowsky
    Commented Apr 28, 2015 at 14:47

2 Answers 2

0

As I understand your question, I recommend you to use for statement, you can do this:

<ul id="slideLinks">
<?php $slide_link = $node->field_slideshow_link['und']; ?>
<?php for ($i = 0; $i < count($slide_link); ++$i): ?>
    <li><a class="slideControl" href="javascript:void(0);"><?php print render($slide_link[$i]['title']); ?></a></li>
<?php endfor; ?>
</ul>

This will print all your titles...

3
  • Thanks, this worked. Just curious, I had it working using foreach in Drupal 6. Any reason why I can't use foreach for Drupal 7?
    – srodrig
    Commented Apr 28, 2015 at 15:11
  • Yes, you can, I just have habit to use for statement to look through array...
    – mixerowsky
    Commented Apr 28, 2015 at 15:14
  • Alright, thanks. I'll try to see if I can get foreach working. I have a similar issue with rendering multiple image url. I've asked the question here: drupal.stackexchange.com/questions/156641/…
    – srodrig
    Commented Apr 28, 2015 at 18:01
0

// Not enough points to comment on the good answer @mixerowsky

Drupal recommends using the field variable directly to avoid problems with language in the array (in your case ['und'] for undefined)

Field variables: for each field instance attached to the node a corresponding variable is defined; for example, $node->body becomes $body. When needing to access a field's raw values, developers/themers are strongly encouraged to use these variables. Otherwise they will have to explicitly specify the desired field language; for example, $node->body['en'], thus overriding any language negotiation rule that was previously applied.

So, extending the answer from @mixerowsky:

<ul id="slideLinks"> <?php foreach ($field_slideshow_link as $link) : ?> <li><a class="slideControl" href="javascript:void(0);"><?php print $link['title']; ?></a></li> <?php endforeach; ?> </ul>

3
  • I think you forgot to use HTML wrappers
    – srodrig
    Commented Apr 28, 2015 at 15:12
  • Thanks, so instead of $node->field_slideshow_link I should just use $field_slideshow_link?
    – srodrig
    Commented Apr 28, 2015 at 18:18
  • If you have a multilingual site set up, then definitely. If you do not, then it is just the proper way to do it. Anyways, the language in drupal arrays is very often an issue. Best stay with the recommended way of doing things. (btw. @mixerowsky's answer was spot on, for any drupal array out there)
    – jojomojo
    Commented Apr 28, 2015 at 18:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.