Drupal 7获取图像字段路径

gle*_*enk 21 drupal drupal-7

我想获得一个字段的图像路径.我在节点中,我需要图像的Url才能将其作为内联css中的背景图像.我找不到解决方案.你能帮助我吗?

Mun*_*ine 51

从它的URI获取图像的路径:

file_create_url($node->field_image['und'][0]['uri']);

有关file_create_url()的更多信息,请访问:http://api.drupal.org/api/drupal/includes%21file.inc/function/file_create_url/7

要使用其URI的图像样式创建图像的路径,请使用:

image_style_url($style, $node->field_image['und'][0]['uri']);

有关image_style_url()的更多信息,请访问:http://api.drupal.org/api/drupal/modules!image!image.module /function/image_style_url/7

  • 这没有考虑多语言领域.请参阅下面的@Gergely的答案.即使您认为您的网站"永远不需要"支持翻译,请不要这样做.有关更多信息,请参阅http://www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way (2认同)

42d*_*Ltd 20

我担心,上述解决方案都不正确.他们不遵循Drupal标准.

// field_video_image is the name of the image field

// using field_get_items() you can get the field values (respecting multilingual setup)
$field_video_image = field_get_items('node', $node, 'field_video_image');

// after you have the values, you can get the image URL (you can use foreach here)
$image_url = file_create_url($field_video_image[0]['uri']);
Run Code Online (Sandbox Code Playgroud)

更多细节:http: //www.computerminds.co.uk/articles/rendering-drupal-7-fields-right-way

  • 上面的代码很完美.一般来说,大多数人不需要像上面所描述的那样做,但是如果你想编写符合Drupal标准的代码并且在事情发生变化时不会破坏,那么就无法强调这是多么重要.参考代码中的项目,如$ object ['und'] [0] ['element']是非常糟糕的做法.始终使用field_get_items或等效项. (4认同)
  • 这是一个古老的问题,但是由于互联网上充斥着不良实践,因此在Drupal中获取字段值时,这一方法应该引起更多关注,因为这是正确的方法。正如Jamie Hollern所写的那样,您永远不要使用很多人推荐的['und'] [0]东西。 (2认同)

Kri*_*fer 5

我有时会用这个:

$node = node_load($nid);
$img_url = $node->field_userimage['und'][0]['uri'];

<img src="<?php print image_style_url($style, $img_url) ?>" />
Run Code Online (Sandbox Code Playgroud)

  • 你也可以使用[theme_image_style()](http://api.drupal.org/api/drupal/modules--image--image--module/function/theme_image_style/7)而不是硬编码`<img> `标签 (3认同)
  • 使用主题函数将是首选:`theme('image_style',array('style_name'=>'name','path'=> $ node-> field_userimage ['und'] [0] ['uri'] ,'alt'=>'替代文字'));` (3认同)