要輸出節點內容有多種方法,自己用的兩種:
1,直接打印數組內容,像這樣:
print $node->title;//打印title
假設有一個字段名叫“field_caption”的長文本字段,
print $node->field_caption['und'][0]['value'];
圖像字段:
print file_create_url($node->field_image['und'][0]['uri']);//函數file_create_url()轉換public://之類的constants,生成實際可用的url。
另外一種方法,使用render方法,要先hide()然後才能render():
use the hide()
function to remove the field from $content, and then therender()
function to print our field somewhere else in the template.
使用hide()函數把要特別打印的字段從$content移除,然後再使用render()函數在模板文件的其他地方打印出來。
For example lets assume we have an image field called field_image,
First hide the field to prevent it printing out in
$content
:
假設我們有一個image字段叫field_image,
首先我們隱藏這個字段以阻止它從$content裏被打印出來。
<?php
hide($content['field_image']);
print render($content);
?>
Then we use the render()
function and the field name to print the field somewhere else:
然後我們使用render()函數把這個字段在其他地方打印出來。
<?php
print render($content['field_image']);
?>
特別注意:在content type裏設置字段顯示屬性的時候要注意,在不同顯示類別下不同字段的可見性,一定要保證該字段在當前顯示類型(比如全部顯示或是“預告片”)裏是顯示的,否則打印不出來,程序也不報錯。