我上传Imagefield的图像,我想添加一个类到<img src="imagefile" class="caption" />
,因为没有为由Imagefield呈现的图像定义的类。如何将一个类添加到由imagefield呈现的img中?
这可能在Drupal 6吗?
我上传Imagefield的图像,我想添加一个类到<img src="imagefile" class="caption" />
,因为没有为由Imagefield呈现的图像定义的类。如何将一个类添加到由imagefield呈现的img中?
这可能在Drupal 6吗?
您可以覆盖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文件。
你究竟想要做什么? 如果你想用css设置你的输出为imagefield字段,你可以使用前一个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;
}
谢谢,但它并没有奏效,也许是因为我使用imagecache用的ImageField和imagecache是压倒一切的ImageField的输出。我试图把代码放在这里,但字符限制不允许我。 :-( – Himanshu
使用[Theme Developer](http://drupal.org/project/devel_themer)模块找出哪个模板/主题功能正在处理imagefield输出 – Alexander
嗨Himanshu,我已经扩展了我的答案,包括正在使用Imagecache的情况。 – Alexander