1

I'm attempting to create a custom field template for an image field with mutliple values. My field name is field_photo_slidshow

My field template name is: field--field-photo-slideshow.php and I've got it coded like this to produce a Flexslider slideshow:

<div class="<?php print $classes; ?>"<?php print $attributes; ?>>
<?php if (!$label_hidden): ?>
<div class="field-label"<?php print $title_attributes; ?>><?php print $label ?>:&nbsp;    </div>
<?php endif; ?>
<div class="flexslider"<?php print $content_attributes; ?>>
<ul class="slides">
<?php foreach ($items as $delta => $item): ?>
<li class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?> 
<!-- <p class="flex-caption">Caption</p> -->
</li>
<?php endforeach; ?>
</ul>
</div>
</div>

And this does what I want so far -- creates and unordered list of the rendered images. But now, I want to add an optional caption, derived from the Image fields alt tag -- but I have no idea how to render the ALT tag from a given image in the above code in the field tempalte. That part of the code, in theory, would something like this:

<?php foreach ($items as $delta => $item): ?>
<li class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?> 
<p class="caption">ALT TEXT FROM IMAGE FIELD</p>
</li>
<?php endforeach; ?>

Doe anyone have any idea out to print out that alt text in the field template code?

thank you, Nathaniel

2 Answers 2

2

If you install Devel module and use dpm($item); in your code, you will see the structure of $item. Alt is in $item['#item']['alt'].

See for example Modify tpl file to print the path to the image instead of the image itself?.

1
  • This was right-on! Thank you so much, I'd been banging my ahead against the wall for 2 hours trying to find the answer, and I couldn't remember what the Devel module was called or its functionality. This was a life-saver!!!
    – pumablues
    Sep 3, 2013 at 1:41
0

For me it works;

print $item['data']['alt'];

So for you rhis:

<?php foreach ($items as $delta => $item): ?>
<li class="field-item <?php print $delta % 2 ? 'odd' : 'even'; ?>"<?php print $item_attributes[$delta]; ?>><?php print render($item); ?> 
<p class="caption"><?php print $item['data']['alt']; ?></p>
</li>
<?php endforeach; ?>

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.