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:

Read More
<?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 🙂

Related posts

2 comments

  1. 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 } ?>
    
  2. 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.

Comments are closed.