Viktor
Viktor

Reputation: 722

How to get dynamically link to thumbnail in Wordpress

How instead of href="img/1.jpg, get the image thumbnail Url with WordPress function..

<a class="popup-link" href="img/1.jpg"></a>

Upvotes: 0

Views: 747

Answers (3)

SHUVO MUSA
SHUVO MUSA

Reputation: 554

Have a look on the function <?php the_post_thumbnail( $size, $attr ); ?> this $size and $attr are 2 Parameters and $size is string/array Image size. Either a string keyword (thumbnail, medium, large, full), or any custom size keyword defined. $attr is array of attribute/value pairs and it's Default: None let inside this how its works $default_attr = array( 'src' => $src, 'class' => "attachment-$size", 'alt' => trim( strip_tags( $wp_postmeta->_wp_attachment_image_alt ) ), 'title' => trim( strip_tags( $attachment->post_title ) ) );
if you just use this without parameter the_post_thumbnail(); then it gives default sizes post_thumbnail and
if you the_post_thumbnail('large' ); then you can see the large size of images and you can changes both of this size via WordPress Administration Media panel under Settings > Media
Now if you <?php the_post_thumbnail( 'anysize', array( 'class' => 'popup-link' ) ); ?>
Or You can use wp_get_attachment_image_src( $attachment_id, $size, $icon ); this functions for getting image URL and then $imgURL = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'full'); <a class="popup-link" href="<?php echo $imgURL[0];?>"></a>

Upvotes: 1

user4094161
user4094161

Reputation:

You can get the image thumbnail Url with WordPress function..

<?php wp_get_attachment_image_src( $attachment_id, $size, $icon ); ?> 

Sample Usage Example :

<?php
$imgarray = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'full');
$imgURL = $imgarray[0];
?>
<a class="popup-link" href="<?php echo $imgURL;?>"></a>

Reference to the codex

https://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src

Upvotes: 1

Prabin Parajuli
Prabin Parajuli

Reputation: 559

You can use get_the_post_thumbnail function.

<a class="popup-link" href="<?php echo get_the_post_thumbnail( $post_id = null, $size = 'post-thumbnail', $attr = '' );"></a>

Parameters

$post_id (int) (Optional) Post ID. Default is the ID of the $post global. Default value: null

$size (string|array) (Optional) Registered image size to use, or flat array of height and width values. Default value: 'post-thumbnail'

$attr *(string|array) (Optional) Query string or array of attributes.

Default value: ''*

For Details Visit https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/

Upvotes: 1

Related Questions