designer-trying-coding
designer-trying-coding

Reputation: 6064

Drupal 7: calling custom content type field into page tpl

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

Upvotes: 0

Views: 11541

Answers (3)

Alireza Tabatabaeian
Alireza Tabatabaeian

Reputation: 169

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.

Upvotes: 0

wildpeaks
wildpeaks

Reputation: 7392

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).

Upvotes: 2

Mahesh
Mahesh

Reputation: 2256

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.

Upvotes: 2

Related Questions