Featured Image Link to WordPress


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" 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];  ?>

“>
<?php the_post_thumbnail(‘medium’, array( ‘class’ => ‘alignleft’ )); ?>
</a>

 

Here is another Handy tool..

http://codex.wordpress.org/Conditional_Tags

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s