1

I have a multi-value CCK field set up using the Link Image Field module, and I need to print all of the images (linked) on the content page. I used the code below to print the images themselves, but cannot figure out how to wrap each image's link around each image.

                <?php if ($node->field_day_option_buttons[0]['filepath']): ?>
             <?php
                foreach($node->field_day_option_buttons as $image) {
                 if (file_exists($image['filepath'])) {
                     print theme_imagecache('my image cache preset name', $image['filepath']);
                    }
                  }
              ?>
            <?php endif; ?>

Can anyone provide input on how to accomplish that?

Thanks!

EDIT Here is the info placed on the page when I run a print(r) on that cck field:

Array ( 
  [0] => Array ( 
    [fid] => 347
    [uid] => 1
    [filename] => days-5.png
    [filepath] => sites/default/files/days-5_0.png
    [filemime] => image/png
    [filesize] => 440
    [status] => 1
    [timestamp] => 1366038083
    [list] => 1
    [data] => Array (
       [alt] => Alt Text
       [title] => Title Text
       [url] => http://www.mysite.com
       [target] => _self
    ) 
    [nid] => 18
  )
  [1] => Array (
    [fid] => 348
    [uid] => 1
    [filename] => days-20.png
    [filepath] => sites/default/files/days-20_0.png
    [filemime] => image/png
    [filesize] => 506
    [status] => 1
    [timestamp] => 1366038108
    [list] => 1
    [data] => Array (
       [alt] => Alt Text
       [title] => Title Text
       [url] => http://www.mysite.com
       [target] => _self
     )
    [nid] => 18
  )
) 
| improve this question | | | | |
  • 1
    what else is available inside $image besides 'filepath', eg can you give us a print_r() or equivalent of the rest of the data you have to work with. I am assuming somewhere in there is the link/url you need to grab. – Jimajamma Apr 15 '13 at 17:20
  • @Jimajamma I added the info from the print(r) to my question above. Does that show what you are looking for? – hockey2112 Apr 15 '13 at 17:31
  • some editing shows me you are probably wanting to grab the $image['data']['url'] info and then use that with @Boriana's answer below. – Jimajamma Apr 16 '13 at 2:08
2

You should be able to use the l() function (link function) to print out your link.

Typically, l() is used with the arguments l("My link text", $path), however, the first argument doesn't have to be text and can be just html. You just have to specify that with the optional $options array argument at the end if you're including html. :-)

Here is more documentation on using l(): http://api.drupal.org/api/drupal/includes!common.inc/function/l/7 The 6 & 7 versions should work similarly.

In your case, you can build your link like this:

  1. Your first argument will be an image, rather than text as the link
  2. Your second argument will be the path you want the image to link to, which should be $image['filepath']
  3. Your third argument should specify that the first argument is html, and not just text

So, try something like:

<?php
print l(  
    theme_imagecache('my image cache preset name', $image['filepath']),
    $image['data']['url'],
    array(
          'attributes' => array(   // Sample attributes you can apply to your anchor tag
            'id' => 'your-link-id',
            'class' => 'your-link-class',
            'target' => '_blank',  // open link in new window.
          ), 
        'html' => true,
    )
);
?>

I haven't tested this yet, as I don't have a Drupal 6 site handy, but this should work. Let us know if it works or if it needs some tweaking...

| improve this answer | | | | |
  • in this, using $image['data']['url'] as the second argument or $path is probably what the OP is asking about. – Jimajamma Apr 16 '13 at 1:59
  • Hmmm. @Jimajamma, I'm just wondering whether to edit my response, but it seems your edit would result in each image to the site's homepage? Is that how how you understood the question? Am I missing something? – Boriana Ditcheva Apr 16 '13 at 3:10
  • I understood the question to be how to print the images wrapped in the link that is set in the field, and the only link I see there is the url one (wherever it happens to point) – Jimajamma Apr 16 '13 at 4:18
  • OK, you're probably right. That's the whole point of the link image module, after all. I misread to mean the image's URL too literally. I'm going to edit my response. Thank you! – Boriana Ditcheva Apr 16 '13 at 13:15
  • 1
    Why not? :-) Done. – Boriana Ditcheva Apr 16 '13 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.