I am theming the node-blog.tpl.php file for my site. I would like to use the description of the attached images as captions under the images. Using the code below the image shows appropriately, but no description is shown. To check to see if the description was the issue or if it was showing up somewhere else in the page I checked via Firebug. I also tested '#weight' which returned 0 under the image where I would have expected.

My first thought was that the image_attach description isn't immediately associated with the current node and that I may need to load the image node to retrieve the description, but I wasn't sure exactly how to go about doing this, since there may be any number of images on the blog page.

I have looked at some of the other 'caption' issues, but the code they used did not seem appropriate in my case.

<div style="float:right;margin:0 20px 0 0;">
     <?php print $node->content["image_attach"]["#value"]; ?>
     <?php print $node->content["image_attach"]["#description"]; ?>
</div>

Comments

lisa_vt’s picture

After doing a little more digging, I found that I'm not actually interested in the #description, but instead the body ("Here is the caption for my picture." in the print_r($node) output below).

I am not sure how to handle stdClass Objects. I read somewhere to use _set_state, but all of the examples I could find were based on dynamically changing the database.

The part that matters:

[image_attach] => Array
        (
            [0] => stdClass Object
                (
                 [body] => Here is the caption for my picture.

Abbreviated output from print_r(node) - to show all instances of [image_attach]:

stdClass Object
(
    [nid] => 1632
    [type] => blog
    [language] => 
    [uid] => 93
    [status] => 1
    [created] => 1303150049
    [changed] => 1304341638
    [comment] => 2
    [promote] => 1
    [moderate] => 0
    [sticky] => 0
    [tnid] => 0
    [translate] => 0
    [vid] => 1637
    [revision_uid] => 93
    [title] => Long blog title to see how it looks in navigation
    [body] => 
<-------------- some removed for brevity-------------------->
    [content] => Array
        (
            [image_attach] => Array
                (
                    [#value] => 
                    [#weight] => 0
                    [#title] => 
                    [#description] => 
                    [#printed] => 1
                )

            [#content_extra_fields] => Array
<-------------- some removed for brevity-------------------->
                (
                    [image_attach] => Array
                        (
                            [label] => Attached images
                            [description] => Image Attach module form.
                            [weight] => 0
<-------------- some removed for brevity-------------------->
    [image_attach] => Array
        (
            [0] => stdClass Object
                (
                    [nid] => 1640
                    [type] => image
                    [language] => 
                    [uid] => 93
                    [status] => 1
                    [created] => 1304017595
                    [changed] => 1304017708
                    [comment] => 2
                    [promote] => 0
                    [moderate] => 0
                    [sticky] => 0
                    [tnid] => 0
                    [translate] => 0
                    [vid] => 1645
                    [revision_uid] => 93
                    [title] => Bobcat
                    [body] => Here is the caption for my picture.
lisa_vt’s picture

Title: image_attach description in tpl file » Creating a caption via image_attach and the tpl file
Status: Active » Closed (fixed)

The following code works:

<?php print $node->image_attach['0']->body; ?>

This took me quite a while to figure out, so I hope the above information can help someone else in the future!

lisa_vt’s picture

Status: Closed (fixed) » Active

So, the above works great, for one picture. When I add the second picture, it does not have a caption. The code below only works for the "0" stdClass Object (the first picture) and the second picture is the "1" stdClass Object. I will need to implement some kind of loop to check for how many stdClass Objects are part of the image_attach array.

<div style="float:right; width:255px; margin: 0 20px 0 0">
     <span><?php print $node->content["image_attach"]["#value"]; ?></span>
     <div style="float:right; text-align:center; width: 225px;margin: 0 10px 0 0">
          <i><?php print $node->image_attach['0']->body; ?></i>
     </div>
</div>

Pay no attention to the div styling.

joachim’s picture

You need a foreach loop -- the templates in Views module have examples of how that works.

lisa_vt’s picture

Status: Active » Closed (fixed)

Thanks Joachim, the views templates were super helpful. Included below is the abbreviated code (the foreach loop to print the image) and the code I actually used (do to some centering issues etc). I included both in case someone else has the weird centering issue.

Abbreviated code:

<?php foreach ($image_attach as $id=>$ids): ?>

<a href="/www.lisa.stat.vt.edu/test/?q=node/<?php print $ids->nid; ?>">
<img class="image image-preview " width="225" title="<?php print $image_attach["title"]; ?>" alt="<?php print $image_attach["title"]; ?>" src="<?php print $ids->images["preview"]?>">
</a>

<i><?php print $ids->body; ?></i>

<?php endforeach; ?>

I had a little bit of trouble getting things to fit and center the way I wanted them, so below you can see that I brute-forced the float for my needs.

Actual code that I used:

<?php foreach ($image_attach as $id=>$ids): ?>

<div style="float:right; width:255px; margin: 0 20px 0 0; display: table;">
<span><a href="/www.lisa.stat.vt.edu/test/?q=node/<?php print $ids->nid; ?>">
<img class="image image-preview " width="225" title="<?php print $image_attach["title"]; ?>" alt="<?php print $image_attach["title"]; ?>" src="<?php print $ids->images["preview"]?>">
</a></span>

<div style="float:right; text-align:center; width: 225px;margin: 0 20px 0 0"><i><?php print $ids->body; ?></i></div>
</div>
<br>

<?php endforeach; ?>
lisa_vt’s picture

The above code creates a "Invalid argument supplied for foreach()" error in the database logging whenever someone views a blog page without an image attached. This is easily fixed with an if statement.

<?php if ($image_attach): ?>
<?php foreach ($image_attach as $id=>$ids): ?>

<a href="/www.lisa.stat.vt.edu/test/?q=node/<?php print $ids->nid; ?>">
<img class="image image-preview " width="225" title="<?php print $image_attach["title"]; ?>" alt="<?php print $image_attach["title"]; ?>" src="<?php print $ids->images["preview"]?>">
</a>

<i><?php print $ids->body; ?></i>

<?php endforeach; ?>
<?php endif; ?>