Code:
//gets all image links with ref to the "product_images" directory
$j("a[href*='product_images/']").each(function() {
//gets the existing href attribute
var linkhref = $j(this).attr("href");
//writes the new link reference to the anchor link element
$j(this).attr("href", linkhref.replace(".png", "a.png"));
newfile = $j(this).attr("href").split('/').pop();
$j(this).attr("href", ("http://www.mywebsite/wp-content/uploads/" + newfile));
});//ends "each" function
I am using $j to call jQuery in no-conflict mode.
Basically I want to just rename some links to a different directory.
I am sure there are much cleaner ways to write this.
For a start, it assumes that the files will always be in .PNG format. But if someone uses a GIF the script breaks.
How to fix that?
Second, is there a more portable way of renaming the new path rather than manually typing the complete path as I have done here?
Last edited by TWD; 11-13-2009 at 07:52 PM..
|