CharlyAnderson
CharlyAnderson

Reputation: 737

Wordpress Featured Image URL

I am trying to add the featured image url to the featured image, so it will open up in a lightbox when clicked.

My code is:

<?php if (have_posts()): while (have_posts()) : the_post(); ?>

<a href="#" target="_blank"><?php if ( has_post_thumbnail() ) { the_post_thumbnail();} ?></a>

<?php endwhile; ?>

    <?php else: ?>

        <article>

            <h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>

        </article><!--/ Article -->

<?php endif; ?>

I have tried quite a few things but cannot get it to pull the thumbnail URL. I have tried adding these to where the link # is:

wp_get_attachment_thumb_url( get_post_thumbnail_id( $post->ID ));         

...

$thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ) );

...

<?php echo $thumbnailsrc ?>

I am sure there is a simple solution that I am yet to find. Thanks to anyone who maybe able to assist :-)

Upvotes: 1

Views: 9444

Answers (2)

Irvin Lim
Irvin Lim

Reputation: 2451

Try this:

wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' )[0]

Assuming you are in The Loop, you can simply use get_post_thumbnail_id() to get the current post featured image ID. You also need to pass the second parameter full in order to get the non-resized version of the image for your lightbox source. You can limit it to large as well, if you like.

Upvotes: 2

Anand
Anand

Reputation: 546

please try this code adding in to your post loop. and confirm you have set feature image for that post which you listings.

<?php $post_thumbnail_id = get_post_thumbnail_id( $post->ID );
if(!empty($post_thumbnail_id)) {
$img_ar =  wp_get_attachment_image_src( $post_thumbnail_id, 'full' ); ?>    
<img src="<?php echo $img_ar[0];?>" />
<?php } ?>

Upvotes: 4

Related Questions