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
Comment #1
lisa_vt commentedAfter 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:
Abbreviated output from print_r(node) - to show all instances of [image_attach]:
Comment #2
lisa_vt commentedThe following code works:
This took me quite a while to figure out, so I hope the above information can help someone else in the future!
Comment #3
lisa_vt commentedSo, 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.
Pay no attention to the div styling.
Comment #4
joachim commentedYou need a foreach loop -- the templates in Views module have examples of how that works.
Comment #5
lisa_vt commentedThanks 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:
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:
Comment #6
lisa_vt commentedThe 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.