drupal7自定义输出节点内容($content里的每一个字段)

要输出节点内容有多种方法,自己用的两种:

 

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里设置字段显示属性的时候要注意,在不同显示类别下不同字段的可见性,一定要保证该字段在当前显示类型(比如全部显示或是“预告片”)里是显示的,否则打印不出来,程序也不报错。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值