Jeevachaithanyan Sivanandan

Featured Image Link to Wordpress

In technology · 1 min read

If you are a WP dev,. surely you will get into situations like you need to get the href link of featured image of WP page / posts  Here the code snippet you can use handy

example

<?php the_post_thumbnail('front', array( 'class' => 'alignleft' )); ?>

Results in something like (depending on which post)

<img width="120" height="80" src="[http://images.mathrubhumi.com/images/2013/Nov/08/03089_536886.jpg](http://images.mathrubhumi.com/images/2013/Nov/08/03089_536886.jpg)" alt="IMG_7506" title="IMG_7506" />

So how do you return just the url for use in a link? The code below will return the image in a link (using the lightbox plugin). the bold part is the PHP function for returning the image url itself.

<a rel="lightbox" href=" **<?php $image_id = get_post_thumbnail_id(); $image_url = wp_get_attachment_image_src($image_id,’large’, true); echo $image_url[0];  ?>** “> ‘alignleft’ )); ?>

 

Here is another Handy tool..

http://codex.wordpress.org/Conditional_Tags