Thoughts and ramblings on tech, media, culture, and food. Plus some other stuff, I'm sure.

Link post_thumbnail in WordPress

You're using the built-in post thumbnail feature in WordPress. That's all well and good, but you'd really like it to link to the originally uploaded image for a lightbox or something else.

How do you do it?

I'm not the best at PHP syntax, so if there's a smarter/cleaner way, please let me know!

First we need to use the getpostthumbnail_id() function like this:

<?php $thumbID = get_post_thumbnail_id(); ?>

This sets the variable $thumbID to the id of your post thumbnail.

Next, we want to build the URI for your link:

<a href="<?php echo wp_get_attachment_url($thumbID); ?>">

Now, there's probably a cleaner way to do this, but again, I'm not all that awesome at syntax. But basically you're using the wpgetattachment_url(); function in conjunction with your post thumbnail's id to get the original image src.


Thanks to @cgrymala over at HTMLCenter for this tip.