1

I have enabled a function when I uploaded an image, it auto create a post with that image as the featured image.

Now I wish to link to that featured image to that of the image file/media file URL so that lightbox effects can work on it. But not sure how to implement this? Any help will be highly appreciative. The function that I have used is -

add_action('add_attachment', 'create_post');
function create_post( $attach_ID ) {

$attachment = get_post( $attach_ID );

$my_post_data = array(
'post_title' => $attachment->post_title,
'post_type' => 'post',
'post_category' => array('0'),
'post_status' => 'publish'
);
$post_id = wp_insert_post( $my_post_data );

// attach media to post
wp_update_post( array(
'ID' => $attach_ID,
'post_parent' => $post_id,
) );

set_post_thumbnail( $post_id, $attach_ID );

return $attach_ID;
}
  • Welcome to WPSE. Just a point of note that will help you in the future, when you add code to your questions/answers, in the editor there is a {} symbol just above it. Add your code to the post, highlight it and click that {} symbol. This will place your code in a code block as in my edit. :) – Pieter Goosen May 25 '14 at 12:15
  • Thanks for the heads up. Will surely keep this in mind next time. – kalyan May 25 '14 at 12:16
1

You would need to edit your themes single.php template file and modify the line of code calling the featured image. It would look something like this:

<?php the_post_thumbnail(); ?>

Replace it with this...

<?php 
 if ( has_post_thumbnail()) {
   $large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large');
   echo '<a class="lightboxclassname" href="' . $large_image_url[0] . '" title="' . the_title_attribute('echo=0') . '" >';
   the_post_thumbnail('thumbnail');
   echo '</a>';
 }
 ?>

Then make sure you replace the class name ('lightboxclassname') with whatever the class name is that your current lightbox script needs to trigger the popup on click.

You can find info on WordPress Codex here: http://codex.wordpress.org/Function_Reference/the_post_thumbnail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.