I want to grab be able pull out images attached to nodes and put them where I like in page.tpl.php.

I thought that this code placed in page.tpl.php would do it, but it doesn't.

<?php print $node -> content['image_attach'][‘#value’]; ?>

While I'm at it, this doesn't work either, just to simply pull the title out:

<?php print $node -> content['title'][‘#value’]; ?>

But this does work:

<?php print $title ?>

What am I doing wrong? I'm sure this is a dumb one.

Comments

VPlex’s picture

What specifically are you trying to do with images attached to a node and what title are you trying to print out? The current node's title or something else?

If you can describe what you need more clearly then I can probably give you a code snippet.

Rick Hood’s picture

I am trying to put images attached to a node where I want to in the page.

The devel module tells me its there in content (see below) but I don't know how to access it.

content

Array
(
    [body] => Array
        (
            [#value] => <p>blah blah</p>

            [#weight] => 0
        )

    [image_attach] => Array
        (
            [#value] => <div style="width: 100px" class="image-attach-body"><a href="/~hinckley/?q=node/3"><img src="http://000.00.00.000/~hinckley/files/images/T55-01.thumbnail.jpg" alt="T55-01" title="T55-01"  class="image image-thumbnail " width="100" height="36" /></a></div>

            [#weight] => 0
        )

)
VPlex’s picture

Obviously I don't know all the variables that exist in the string you're trying to print but I'm a fan of taking a more direct route. This will just look for attached images and create based on that. Not sure where each image will be linking too, but you could always use the description field to populate that instead of the title. Hopefully this will at least get you started.


foreach ($node->files as $files)
{
	$fileType		= $files->filemime;
	if($fileType == 'image/pjpeg')
	{
		$imagePath = $files->filepath;
		$imageTitle = $files->description;						 
		print '<div style="width: 100px" class="image-attach-body"><a href="'.$someVariable.'"><img src="/'.$imagePath.'" alt="" title="'.$imageTitle.'"  class="image image-thumbnail " width="100" height="36" /></a></div>';
	}
}	

justin_d’s picture

Amazing work ,

I'm a fan of taking a more direct route.

i appreciate this approach even. Though in drupal everything is done via module/hooks etc..
but sometimes i am realy stuck to using this extended APIS and opt to code manually as u did.

great work done .

pal_ur’s picture

Placeing Your code in page.tpl.php shows up only one picture of the attached two pictures. Funny, but print_r($node->files) or print sizeof($node->files) shows, that there are eg. two files, but foreach($node->files as $files) { .... } prints up only one, the second one... What did I wrong?

Thx a lot.

doublejosh’s picture

Anyone have any luck finding the path for image attach?

It seems that my attached images are not listed in $node->files.

Also, great work on your code above. THANKS.

Rick Hood’s picture

This works fine:

<?php print $node -> content['image_attach'][‘#value’]; ?>

but you have to put it in the node tpl file, not the page tlp file! Dumb.

coupet’s picture

----
Darly

DaveS98’s picture

Hi all,
I've been reading this article and tried to print an attached image but it doesn't display anything.

Here is my original post which seems to going nowhere.
http://drupal.org/node/215081

print $node -> content['image_attach'][‘#value’];

Doesn't work for me.

Any help?

Rick Hood’s picture

Are you putting this code in your page.tpl.php file? If so, thats the problem. It goes in node.tpl.php. Its confusing because there is a $content variable in both places - but they are different.

kappaluppa’s picture

I had to change the tick marks around #value, and then it worked - kind of...

I don't see the image when I look at the page. However, when I look at the code with firebug, the img src is there. But in the HTML it is grayed out and does not display. I can't give a link to the site as it is for a client and is pw protected. The whole div is grayed out, actually.

<div class="view-image-attach-events">
<div class="image-attach-body" style="width: 133px;">
<a href="/node/526">
<img class="image image-_original" width="133" height="28" title="Power & Cooling Event logo" alt="Power & Cooling Event logo" src="http://209.235.229.251:4552/files/images/event_logo_broadgroup.gif"/>
</a>
</div>
</div>

Everything after

and before the last

tag is gray in firebug.

kappaluppa’s picture

the image-attach-body class had a display:none on it...
SOLVED! lol

halfiranian’s picture

Mate, this may sound simple, but I spent *ages* trying to figure this out.

I read a million different pages but finally here's the answer.

Thanks mate

ktleow’s picture

Why don't you use the Contemplate module? You can get the exact code to print all the variables that the node has.