Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

As mentioned on wordpress.org, it is possible to link a thumbnail to its larger self.

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

But I can't find how to make this link modifiable by plugins. I'm using Simple Lightbox but I regularly had to change in the last years.

Currently I get a link to the larger image, I would like to see it in lightbox like all the other images in articles.

I don't feel satisfied with the other answers on stackexchange. They say that we should add manually "rel=lightbox" in the templates

For the moment I added to function.php

function CJG_add_lightbox_to_achor(){
    return 'rel="slb"';
}
share|improve this question
1  
Check this post out. You may want to visit the codex page to check jQuery it's correct still. It does automatically add the rel to the images so users don't have to add it manually each time: Any good lightbox-like option that works in WP 3.2.1?. –  Jeremy Jared May 7 '12 at 16:31
    
Adding rel="lightbox" only works if you've enqueued a lightbox script to load on your site. You could also enqueue "thickbox" with ships with WordPress and use rel="thickbox". –  mrwweb May 7 '12 at 17:45
    
Follow the link I provided to see my solution. –  Jeremy Jared May 8 '12 at 4:34
    
Thank you guys, I posted my solution based on your help –  Christian May 8 '12 at 7:21

1 Answer 1

up vote 0 down vote accepted

Thank you Jeremy Jared. I applied the following solution, based on yours ( Any good lightbox-like option that works in WP 3.2.1? )

First of all, now I call my thumbnail with the following code instead of trying to add the link myself:

wp_get_attachment_link(get_post_thumbnail_id(), 'thumbnail');

Then, I add your filter in function.php

/*************************
*        Lightbox        *
**************************/
  //can be replaced in any child template
if (!function_exists('CJG_add_lightbox_to_achor')) {
    function CJG_add_lightbox_to_achor(){
        return 'rel="slb"';
    }
}

add_filter('wp_get_attachment_link' , 'CJG_lightbox_rel');
function CJG_lightbox_rel($attachment_link ) {
    $attachment_link = str_replace( 'a href' , 'a '.CJG_add_lightbox_to_achor().' href' , $attachment_link );
    return $attachment_link;
}

It is not 100% what I expected, I wanted the plugins to work without any particular add because I might change one day and this solution makes me change it for each website instead of a general change on my Wordpress Network. But it's still a lot better than anything that I had done before.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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