It would be great if a caption could be added to the images so that it would appear within the imagefield div.

Comments

quicksketch’s picture

Your best bet here is to use the image title as a caption. You can use contemplate module or theme_imagecache (if using imagecache) to display the title underneath the image rather than as it's title attribute. I think this has already come up in discussion and vetoed, though I can't find the original issue.

Robardi56’s picture

Can you please give an example with Contemplate ?

I currently add something there but it loops and display first title only once. This is a problem because I have multiple image.

THnaks,
Brakkar

quicksketch’s picture

Category: feature » support

Yep sure can do :)

Assume your field name is called 'image', and therefore 'field_image' in the mind of CCK:

<?php foreach ($node->field_image as $image): ?>
      <div class="field-item">
         <?php print $image['view'] ?>
          <div class="caption"><?php print check_plain($image['title']) ?></div>
       </div>
<?php endforeach; ?>
aether’s picture

Thanks for the insight and suggestions. I have been trying to accomplish this with theme_imagecache and my weak php skills with no luck so far. An example here would be appreciated as well. Thanks again.

quicksketch’s picture

Theme imagecache is definitely a way to go also. Put this in your theme's template.php file:

function garland_imagecache($namespace, $path, $alt = '', $title = '', $attributes = NULL) {
  $attributes = drupal_attributes($attributes);
  $imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $path);
  $output = '<img src="'. $imagecache_path .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
  $output .= '<div class="caption">'. check_plain($title) .'</div>';
  return $output;
}

Replace garland_ with the name of your theme followed by an underscore.

aether’s picture

Thank you for this. Works like a charm. I had a CSS border around my image that was rendering even when there was no image present in the node. I added an if conditional so that no html is output if there is not an image present.

<?php
function garland_imagecache($namespace, $path, $alt = '', $title = '', $attributes = NULL) {
  if ($path) {
    $attributes = drupal_attributes($attributes);
    $imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $path);
    $output = '<img src="'. $imagecache_path .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
    $output .= '<div class="caption">'. check_plain($title) .'</div>';
    return $output;
  }
}
?>
dopry’s picture

Status: Active » Closed (fixed)

Yep those theme functions are powerful... :)

simonlagneaux’s picture

This is exactly what I needed! Thanks, you made my day!

tokko’s picture

Requests for adding captions seem to pop up once in a while. Can anyone remember why this request keeps on getting vetoed? A caption would be a logical feature for an imagefield.

Anyway, if one wants to avoid adding extra modules for doing this, adding the feature (an easily css-themable div with a caption) requires only modding one line in imagefield.module. Of course this is bad practice if forking ImageField is not the intention, but that might become practical as well if there's no desire for the IF people to add the feature officially?

brambor’s picture

@tokko,

Which one line of code would that be?!? I'm not really at a point with Drupal where I should be hacking modules, but I'm going crazy trying to find a workable way to add the fields 'caption' and 'credit' in addition to 'title'. The lack of an ability to add attribution and captioning really limits the utility for me, of what is otherwise a pretty slick module.

Any suggestions you (or others) might have would be most appreciated.

BTW - I'm using Drupal 6.1 - not sure if it is bad form to change that in the "Edit issue settings" above.

geerlingguy’s picture

I added the code from #5 to my template.php, which gracefully adds captions to all the areas I need it to; however, the captions are also showing in areas I'd rather *not* have a caption, such as on the front page. Here's the site in question:

http://stlouisreview.com/

I was wondering if you could help me figure out what I need to do to make the caption only appear on article-type pages... I tried wrapping $output .= '<div class="caption">'. check_plain($title) .'</div>'; in a "if (!$is_front) wrapper, but that didn't do anything.

I tried this:

if (!$is_front) {
  $output .= '<div class="caption">'. check_plain($title) .'</div>';
}

But that didn't seem to work. It still showed up, after clearing the theme registry.

bcobin’s picture

Version: 5.x-1.1 » 6.x-3.9

Thank you, thank you, thank you for this. Changing to current release version of 6.x because #6 works nicely with current version. Thank you again!

himagarwal’s picture

I added the #6 code to template.php and it works fine. Now, when I remove it I'm getting errors.

warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'simple_imagecache' was given in /home/site/simple/includes/theme.inc on line 668.

There were several error lines like above (maybe 20) but for ease I removed all as all of them were exactly same.

How to correct this error?

himagarwal’s picture

Ok! Flushed the cache, and everything now seems to be working fine.

However, such error should not be there, isn't it?

himagarwal’s picture

I added the #6 code to template.php but all the imagecache has now caption. How can I specify a particular or multiple imagecache in this code?

mlehew’s picture

How can the #6 code be modified to work in D7? This seems like it would be a clear-cut solution to solve the captions problem, but I'm not skilled with custom PHP.

jackalope’s picture

himagarwal: I also needed only certain imagecache presets to display the caption. Here's how I got the code to work so that the caption is only displayed for the preset named "fullnode":

function garland_imagecache($namespace, $path, $alt = '', $title = '', $attributes = NULL) {
  if ($path) {
    $attributes = drupal_attributes($attributes);
    $imagecache_path = file_create_url(file_directory_path() .'/imagecache/'. $namespace .'/'. $path);
    $output = '<img src="'. $imagecache_path .'" alt="'. check_plain($alt) .'" title="'. check_plain($title) .'" '. $attributes .' />';
    if ($namespace == 'fullnode') {
      $output .= '<div class="caption">'. check_plain($title) .'</div>';
    }
    return $output;
  }
}
asb’s picture

Issue summary: View changes

Why can't it be a field formatter setting to display a caption? To my understanding, that's what field formatters are made for.