0

I'm working on a Drupal 7 website. I need custom layout for some pages. so I created page--customContentTypeName.tpl.php file and it addresses perfectly.

The problem is, I need to display some fields in page tpl. The code below works fine in node tpl, but page tpl :/

<?php print $content['field_images']['#items']['0']['filename']; ?>" />

How can I call custom fields into page tpl?

Appreciate helps!! thanks a lot!!


** SORTED **

with custom field editing... here is the tutorial video: http://lin-clark.com/blog/intro-drupal-7-theming-fields-and-nodes-templates#comment-54

designer-trying-coding
  • 5,994
  • 17
  • 70
  • 99

3 Answers3

2

The structure changed in 7, the field is keyed by language first ("und" by default which means "undefined"), then you can follow this example:

// Array of Image values
$images = $node->field_images['und'];

//If you don't know the language, the value is stored in:
$node->language


// First image
$image = $images[0];



// If you need informations about the file itself (e.g. image resolution):
image_get_info( $image["filename"] );


// If you want to access the image, use the URI instead of the filename !
$public_filename = file_create_url( $image["uri"] );


// Either output the IMG tag directly,
$html = '<img src="'.$public_filename.'"/>';

// either use the built-in theme function.
$html = theme(
    "image",
    array(
        "path" => $public_filename,
        "title" => $image["title"]
    )
);

Note the usage of the uri instead of the filename for embedding the image in a page because the File API in Drupal 7 is more abstracted (to make it easier to integrate with CDN services).

wildpeaks
  • 7,273
  • 2
  • 30
  • 35
  • Also note that in teaser mode (e.g. a taxonomy term page that lists many nodes), such fields are not provided in variable $node by default. – wildpeaks Mar 04 '11 at 16:52
  • By the way, the Drupal-specialized area of StackOverflow will open to public beta in a few days: http://drupal.stackexchange.com – wildpeaks Mar 04 '11 at 16:54
2

For page.tpl.php if you access the node directly you can use $node variable

$node['field_images']['und'][0]['filename']

else use $page variable.

$page['content']['system_main']['nodes'][1]['field_images']['#items'][0]['filename'];

but remember in a page variable you might have more than one node.

Mahesh
  • 2,186
  • 16
  • 17
0

there are 2 useful modules in drupal for theme developers: Devel and Theme_developer Devel module provides a function called dsm() . using dsm you can recognize that how elements are stored in different objects. like nodes or ... for example you can use this statement : dsm($node) the structure of any nodes in the page will show up in the message box. you can type the statements in your codes.