如何将 HTML 属性添加到 CCK 图像字段 [英] How to add an HTML attribute to a CCK image field

查看:18
本文介绍了如何将 HTML 属性添加到 CCK 图像字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将id="background"属性添加到实际图像标签中.

I am trying to add an attribute of "id="background" to the actual image tag.

<?php print render($content['field_bg_image']); ?>

我查阅了大量文章,它们都与 hook_form_alter 相关,而这不是一个表单.这只是一个图像字段,我只需要一个带有背景值的 ID 属性.我知道我可以使用 javascript,但我想使用 Drupal 并且不再添加任何 javascript.

I've looked up tons of articles and they are all concerned with hook_form_alter and this is not a form. This is simply just an image field, I just need an ID attribute on it with a value of background. I know I can use javascript but I want to use Drupal and not add any more javascript.

推荐答案

图像渲染通过 theme_image_formatter() 不允许你设置属性.

The image render is run through theme_image_formatter() which doesn't let you set attributes.

您可以通过手动构建图像来解决此问题:

You can get around this by building the image up manually:

$img = theme('image', array(
  'path' => $content['field_bg_image'][0]['#item']['uri'],
  'alt' => $content['field_bg_image'][0]['#item']['alt'],
  'attributes' => array('id' => 'background')
));

$content['field_bg_image'][0] = array(
  '#markup' => $img
);

echo render($content['field_bg_image']);

这是未经测试的,所以如果您有任何问题,请告诉我

That's untested so let me know if you have any problems

这篇关于如何将 HTML 属性添加到 CCK 图像字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆