Support » Plugins » Hacks » show a link to download the post image

  • Ary

    (@taramash11)


    Hi ,
    I want to show the download link of the post image ,
    so far, this code returns the cropped image that I want from the media in the post

    <?php the_post_thumbnail('thumbnail-name3', '222', '444', array( "center", "center")) ?>

    but I want to show the download link for the above thumb image in the post !

    can anyone tell me how can I do this ?

    Thanks

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hello Ary,

    I bet you could adapt this to display the download link.

    <?php
    if ( has_post_thumbnail() ) {
    	$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
    	echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '">';
    	the_post_thumbnail( 'thumbnail' );
    	echo '</a>';
    }
    ?>

    https://codex.wordpress.org/Function_Reference/the_post_thumbnail#Post_Thumbnail_Linking_to_Large_Image_Size

    Ary

    (@taramash11)

    Thanks Schulte ,
    I am getting close , how can I modify the code you gave me in a way , for example
    a text would be shown and it would be linkable to the media image file ?
    i.e :

    a linkable text appear like ‘Dowmload’
    and when a user click it , the image file downloads as popup .

    Thanks

    This would provide a link to the larger image that will appear in the window. The user would have the option to download it on their own from the link itself or after viewing the image.

    <?php
    if ( has_post_thumbnail() ) {
    	$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
    	echo '<a href="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '">Download</a>';
    }
    ?>
    Ary

    (@taramash11)

    Dear Schulte ,
    Thank you very much , the code works awesome .

    there is only one thing , how can the image file opens in a new windows ,

    i.e : when a user click on “Download” the image would popup in a new window.

    Thanks alot

    <?php
    if ( has_post_thumbnail() ) {
    	$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
    	echo '<a href="' . $large_image_url[0] . '"target="_blank" title="' . the_title_attribute( 'echo=0' ) . '">Download</a>';
    }
    ?>

    Done. Please keep in mind that spawning a new window does break the back button and leads to problems with accessibility.

    Ary

    (@taramash11)

    Thanks a lot Schulte
    That was exactly what I want .

    Best regards my dear , god bless you .

    Ary

    (@taramash11)

    Dear Schulte ,
    can you modify the above code in a way , when a user click on “Download” the image would download right away ?
    I mean , without the popup , when the user click on Download then the Download prompt would appear ?

    I really appreciate your efforts .
    Thanks .

    You could use the download attribute, but it won’t work in all browsers.

    http://www.w3schools.com/tags/att_a_download.asp

    <?php
    if ( has_post_thumbnail() ) {
    	$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
    	echo '<a href="' . $large_image_url[0] . '" download="' . $large_image_url[0] . '" title="' . the_title_attribute( 'echo=0' ) . '">Download</a>';
    }
    ?>
    Ary

    (@taramash11)

    Many Thanks Schulte
    you saved me .
    The code works very well !

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘show a link to download the post image’ is closed to new replies.