2

The goal is when you click "Download for web" it downloads that image, unfortunately when i click my link it just reloads the page? Any thoughts?

Here is the link to the webpage. http://www.redrocketgraphicdesign.co.uk/test/FIRST/product/firststudent1/

This is the code that I have or the moment:

<div class="Downloads">
<h3>Download image</h3>
<a href="<?php $image_id= woocommerce_get_product_thumbnail();
$image_url = wp_get_attachment_image_src($image_id,'web');
echo $image_url=$image_url[0]; ?>"><img src="<?php bloginfo ('template_url' )?>/img/downloadForWeb.png" alt="Download for web" /></a>

Paolo Forgia
  • 6,572
  • 8
  • 46
  • 58

2 Answers2

1

You have some error in your anchor, is displaying an empty href, make sure your php code is correct.

About your question how to download an image from an achor tag you can just add the attribute download like this:

<a href="/patch/to/your/image" download>Click here to download</a>

With your code should be like:

<div class="Downloads">
<h3>Download image</h3>
<a href="<?php bloginfo ('template_url' )?>/img/downloadForWeb.png">Downlaod for the web</a>
Troyer
  • 6,765
  • 3
  • 34
  • 62
  • Thanks @Troyer, I've added your snippet of code to the site, unfortunately it is showing an error 404 instead of downloading the image? http://www.redrocketgraphicdesign.co.uk/test/FIRST/product/firststudent1/ ( The second download image ) – Anthony Cdf Whitefield Aug 17 '17 at 14:44
  • 1
    @AnthonyCdfWhitefield Sorry, cant help you anymore without the source code, you just need to figure how to put the proper path to the image into the href. – Troyer Aug 17 '17 at 14:46
1

You are getting a 404 error because woocommerce_get_product_thumbnail() is not finding any image to return. I'm assuming you want to download the featured image for the product, so you should be using wp_get_attachment_image_src() instead of woocommerce_get_product_thumbnail(), so change your code to:

<div class="Downloads">
    <h3>Download image</h3>
    <?php 
    // replace $postID with your post id variable
    // change the 2nd parameter to specify the size of the image to get:
    $image_attrib = wp_get_attachment_image_src( get_post_thumbnail_id($postID),'full');
    ?>
    <a href="<?php echo image_attrib[0]; ?>">
        <img src="<?php bloginfo ('template_url' )?>/img/downloadForWeb.png" alt="Download for web" />
    </a>
</div>

If that doesn't work, I'm not sure how much more we can help without seeing more of the relevant source code. As you are already displaying the photo on the page, check the code that displays it to see how it's retrieving the correct url and use it.

NOTE: Your code is trying to display an image (downloadForWeb.png) as the link to download the main image. However downloadForWeb.png doesn't exist at the url in the code, so you are seeing the text "Download for web" instead. You should either fix the url or just use a text link.

FluffyKitten
  • 13,824
  • 10
  • 39
  • 52
  • Thank you, I had to re-jigg one line of code but now working as i desired! Thank you for all your support guys!

    Download image

    ID),'full'); ?> Download for web
    – Anthony Cdf Whitefield Aug 18 '17 at 15:20
  • You just needed to change `$post->ID to` your post id variable (see updated code). Your example didn't include that so I included the default just as an example. You can use either `wp_get_attachment_image_src` *or* `wp_get_attachment_url`, both work (as long as you use the correct post id!!), I use the first because you can tell it which image size you want (thumbnail, medium, full or even a custom size). Including both means you're getting the image twice. If everything is working, can you mark one of the answers as accepted so the question is closed? – FluffyKitten Aug 19 '17 at 01:10
  • @AnthonyCdfWhitefield As the answer worked for you (in as much as it could based on the code you provided), can you accept it so it is resolved on the site (and also we'll both get rep points :) ) – FluffyKitten Aug 25 '17 at 11:31