2011-03-04 48 views
0

我正在使用Drupal 7網站。我需要一些頁面的自定義佈局。所以我創建了頁面 - customContentTypeName.tpl.php文件,它的地址完美。Drupal 7:將自定義內容類型字段調入頁面tpl

問題是,我需要在頁面tpl中顯示一些字段。下面的代碼在節點tpl中工作正常,但頁面tpl:/

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

如何在頁面tpl中調用自定義字段?

鑑賞幫助!非常感謝!!


** SORTED **

與自定義字段編輯......這裏是視頻教程:http://lin-clark.com/blog/intro-drupal-7-theming-fields-and-nodes-templates#comment-54

回答

2

在7改變的結構,該場是由語言第一鍵控( 「und」默認意味着「undefined」),那麼你可以按照這個例子:

// 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"] 
    ) 
); 

請注意使用uri而不是filename用於將圖像嵌入到頁面中,因爲​​(爲了更容易與CDN服務集成)。

+0

另請注意,在前導模式下(例如,列出了許多節點的分類術語頁面),默認情況下變量$ node中不提供此類字段。 – wildpeaks 2011-03-04 16:52:23

+0

順便說一句,StackOverflow的Drupal專用區域將在幾天內公開測試:http://drupal.stackexchange.com – wildpeaks 2011-03-04 16:54:59

2

對於page.tpl.php中 如果訪問節點直接可以使用$節點變量

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

否則使用$ page變量。

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

但記住一個頁面變量,你可能有多個節點。

0

在主題開發人員的drupal中有2個有用的模塊: Devel和Theme_developer Devel模塊提供了一個名爲dsm()的函數。使用dsm的 您可以識別如何將元素存儲在不同的對象中。 像節點或... 例如,您可以使用此語句:dsm($ node) 頁面中任何節點的結構將顯示在消息框中。 您可以在代碼中輸入語句。

相關問題