zyrag
zyrag

Reputation: 353

how to use URL of upload featured image in wordpress

in wordpress I am creating a theme but I need a help,

<? the_post_thumbnail();?>

when I do that it gives me the entire <img src=#>.....

everything is fine so far but I want to do something like

<a href="UPLOADEDIMAGEURL"><? the_post_thumbnail();?></a>

Hence, I need to take uploaded Image URL and wrap it with anchors then replace the uploaded image url how can I do that ? I have tried some in jQuery but I think I can also do that from php also ?

Here is the jQuery version (for that I couldnt take the img src attribute and replace to a href.)

$('.floatedImg').find('img').wrap( "<a href='#' class='colorbox'></a>" );

Solution 1 : can be handled via jQuery Solution 2 : can be via php

Regards

Upvotes: 0

Views: 337

Answers (2)

cpilko
cpilko

Reputation: 11852

Try this:

<?php
$img_url = wp_get_attachment_img_src( get_post_thumbnail_id(), 'thumbnail');
?>
<a href="<?php echo $img_url[0]; ?>"><?php the_post_thumbnail();?></a>

For reference:

Upvotes: 1

MonsieurMechant
MonsieurMechant

Reputation: 9

You can put someething like this in your functions.php file

function get_thumbnail_url() {
    if ( has_post_thumbnail() ) {
        $thumb_url = wp_get_attachment_img_src( get_post_thumbnail_id(), 'thumbnail');
        return $thumb_url[0];    
    }
    else {
        return get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-default.jpg';
    }
}

and then

<a href="<?php echo get_thumbnail_url(); ?>"><? the_post_thumbnail();?></a>

Upvotes: 0

Related Questions