从 Drupal 的链接字段中提取 URL?
我有一个由 URL 和标题组成的 链接字段,我需要打印出来只有在我的节点内容类型 tpl 文件中没有标题的链接字段的 URL,这可能吗?
I have a link field that is composed from a URL and the title, I need to print out only the URL of the link field without the title in my node content type tpl file, is that possible ?
谢谢!
推荐答案
应该很简单:
$url = $node->field_name_of_field[$node->language][0]['url'];
我会分解一下:
字段是节点对象的成员,总是以 field_
为前缀,因此可以使用 $node->field_my_field找到名为
my_field
的字段代码>.
Fields are members of the node object and are always prefixed with field_
so a field called my_field
can be found with $node->field_my_field
.
节点对象的每个字段成员本身就是该字段的所有不同语言版本的数组,由语言键键控.要访问表示节点的语言的字段值,您将使用:$node->field_my_field[$node->language]
或 $node->field_my_field[LANGUAGE_NONE]
(这是默认设置).
Each field members of the node object is itself an array of all different language versions for the field, keyed by the language key. To access the field value for the language that the node is denoted as you would use: $node->field_my_field[$node->language]
or perhaps $node->field_my_field[LANGUAGE_NONE]
(which is the default).
此外,如果字段的基数大于 1,则每个语言数组中可能有多个字段值.如果您有一个允许多个值的字段(例如图像),您将像这样遍历每个值:
Further to that, each language array can potentially have multiple field values in it, if the cardinality of the field is greater than 1. If you have a field (e.g. images) with multiple values allowed you would run through each like this:
foreach ($node->field_my_field[$node->language] as $delta => $item) {
}
语言数组的每个项目中都是实际的字段值.字段可能有多个列(例如链接模块有 url
、title
和 attributes
).要继续前面的示例,您会找到如下所示的 url 和标题:
Within each item of the language array are the actual field values. Fields may have multiple columns (for example the link module has url
, title
and attributes
). To continue with the previous example you would find the url and title like this:
$url = $node->field_name_of_field[$node->language][0]['url'];
$title = $node->field_name_of_field[$node->language][0]['title'];
希望有帮助!
相关文章