Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By panakour on
I want to add href to all img tags on my drupal post. My bellow code is working fine on html page but on drupal doesn't work(do not do anything):
Code:
<?php
$doc = new DOMDocument;
$doc->load($node->body[LANGUAGE_NONE][0]['value']);
$images = $doc->getElementsByTagName('img');
foreach ($images as $img) {
$link = $doc->createElement('a');
$link->setAttribute('href', 'http://www.example.com');
$img->parentNode->insertBefore($link, $img);
$link->appendChild($img);
}
echo $doc->saveHTML();
?>
what could be wrong?
Thank you!
Comments
What's the resulting output?
What's the resulting output?
I have put link to all img
I have put link to all img tags with jquery with bellow code
My problem now is that all node_url are the same. The above code take the last node url and puting in all img while i want put separate link for each image. Is there a solution?
Well, you are setting the URL
Well, you are setting the URL for every link the same here:
You'll have to set the URL dynamically, rather than hard-coding it like you have.
This isn't related to the
This isn't related to the original topic, as you are not using DOMDocument, so you should open a new thread for your question.