Akis Drakopoulos
Akis Drakopoulos

Reputation: 3

Replace all image "<a href" url, with the picture url from "<img src"

i use wordpress and i have a rss posting issue. some image for linking having the external post url and if a user clicks they open the external website.

I try to replace with php all image links with the img source but without luck.

in example i have a post with 2 images:

"blah blah blah"
<a href="http://google.com/thank-you.html"><img src="http://yahoo.com/img/top.jpg" alt="" /></a>
"blah blah blah"
<a href="http://google.com/index.html"><img src="http://yahoo.com/img/bottom.jpg" alt="" /></a>

I wanna to transform this into this:

"blah blah blah"
<a href="http://yahoo.com/img/top.jpg"><img src="http://yahoo.com/img/top.jpg" alt="" /></a>
"blah blah blah"
<a href="http://yahoo.com/img/bottom.jpg"><img src="http://yahoo.com/img/bottom.jpg" alt="" /></a>

Also replace img linking one by one with php.

Thank you

Upvotes: 0

Views: 1747

Answers (3)

rajesh kakawat
rajesh kakawat

Reputation: 10906

try something like this

    jQuery('.img').each(function(){
        jQuery(this).parent('a').attr('href', this.src);
    });

html

    <img class="img" src="http://yahoo.com/img/top.jpg" alt="" />

Upvotes: 1

Bhupendra
Bhupendra

Reputation: 258

try this

html

<a href="http://google.com/thank-you.html"><img class="image" src="http://yahoo.com/img/top.jpg" alt="dsfds" /></a>
<a href="http://google.com/index.html"><img class="image" src="http://yahoo.com/img/bottom.jpg" alt="ad" /></a>

jQuery:

jQuery('.image').each(function(){
    var src = jQuery(this).attr('src');
    jQuery(this).parent('a').attr('href', src);
});

Upvotes: 0

hizbul25
hizbul25

Reputation: 3849

Try with this:

<script>
var getSrc = function(imgSource) {
var img = new Image();
img.src = imgSource;
return img.src;
};
$( "a" ).attr({
href: getSrc,
});
</script>

Upvotes: 0

Related Questions