1

I am trying to add some html code before each tag printed as an array element.  
My code:

  $term_links = array();

  foreach ($vars['node']->taxonomy as $term) 
  {
    $term_links[] = l($term->name, 'taxonomy/term/' . $term->tid,
      array(
        'attributes' => array(
          'title' => $term->description
    )));
  }

  $vars['node_terms'] = implode(', ', $term_links);

At the moment the tags are printed seperated by a comma. I would like to add a small image before each tag element using img src="tag.png" How can I do this?

EDIT - My current Code, still not working.

 if (module_exists('taxonomy')) {

$img  = 'some html';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;


$term_links = array();
foreach ($vars['node']->taxonomy as $term) {



  $term_links[] = l($text, $path, array(
    'html' => TRUE,
      'attributes' => array(
        'title' => $term->description
    )));
 }
   $vars['node_terms'] = implode(', ', $term_links);
 }
}
1
  • 1
    Move these lines: $img = 'some html'; $text = $img . $term->name; $path = 'taxonomy/term/' . $term->tid inside foreach () loop... Commented Jun 10, 2012 at 21:27

1 Answer 1

2

Dupal's l() function has an option "html" you can set it to TRUE and use IMG + TITLE as a title.

Here is the example:

$img  = '<img src="..." />';
$text = $img . $term->name;
$path = 'taxonomy/term/' . $term->tid;

$term_links[] = l($text, $path, array(
  'html'       => TRUE,
  'attributes' => array(
    'title' => $term->description
  )
));
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, I've changed the code to yours but now it only prints the images, but no tags...
Code i posted must be inside foreach() loop, in your example it's outside of loop. Obviously there is no term->name before you enter the foreach() loop.
Ah, you're right! Many thanks! Using this method, can I also add Html code at the end of each tag?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.