我正在使用 Imagefield 上传图像,并且我想添加一个类,<img src="imagefile" class="caption" />
因为没有为 Imagefield 呈现的图像定义类。
这在 Drupal 6 中可行吗?
我正在使用 Imagefield 上传图像,并且我想添加一个类,<img src="imagefile" class="caption" />
因为没有为 Imagefield 呈现的图像定义类。
这在 Drupal 6 中可行吗?
你到底想做什么?如果您尝试使用 css 为图像字段字段设置输出样式,您可以只使用我想的前一个 div 的类。因此,如果您的 html 看起来像这样(我的,使用 imagefield 和 imagecache):
<div class="field field-type-filefield field-field-images"> <!-- created by imagefield -->
<div class="field-items">
<div class="field-item odd">
<a class="imagecache imagecache-thumbs imagecache-imagelink imagecache-thumbs_imagelink" href="http://yoursite/sites/default/files/originalFile.jpg">
<img alt="" src="http://yoursite/sites/default/files/imagecache/thumbs/originalFile.jpg">
</a>
</div>
</div>
</div>
在您的 css 文件中使用类似这样的东西来设置您的图像样式(此示例将图像彼此相邻而不是彼此下方,创建某种画廊)。
.field-field-images img
{
float: left;
margin-right: 8px;
margin-bottom: 8px;
}
您可以覆盖theme_imagefield_image()函数,将其复制到您的 template.php并将其重命名为 MYTHEME_imagefield_image()。它会是这样的:
function MYTHEME_imagefield_image($file, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE) {
//
// ... same contents as the original one, except:
//
$attributes['class'] = $attributes['class'] ? "{$attributes['class']} my-custom-class" : 'my-custom-class';
$attributes = drupal_attributes($attributes);
return '<img '. $attributes .' />';
}
...在您的template.php中。
并清除缓存!:>
编辑:
如果您使用的是Imagecache模块,那么您将改写theme_imagecache()函数,最终得到:
function MYTHEME_imagecache($presetname, $path, $alt = '', $title = '', $attributes = NULL, $getsize = TRUE, $absolute = TRUE) {
// Check is_null() so people can intentionally pass an empty array of
// to override the defaults completely.
if (is_null($attributes)) {
$attributes = array('class' => 'imagecache imagecache-'. $presetname);
}
if ($getsize && ($image = image_get_info(imagecache_create_path($presetname, $path)))) {
$attributes['width'] = $image['width'];
$attributes['height'] = $image['height'];
}
// These two lines are what you need
$custom_class = 'your-custom-class';
$attributes['class'] = $attributes['class'] ? "{$attributes['class']} {$custom_class}" : $custom_class;
$attributes = drupal_attributes($attributes);
$imagecache_url = imagecache_create_url($presetname, $path, FALSE, $absolute);
return '<img src="'. $imagecache_url .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
}
...在您的template.php文件中。