Exclude featured image from gallery code stops Lightbox from working

I was looking for a solution to exclude the featured image/thumbnail in each post, so I found this great piece of code and added it to functions.php. It worked perfectly except for one thing: it renders the Lightbox jquery plugin useless.

The difference I see in code is this. With the code:

Read More
<a href='http://site.com/wp-content/uploads/2012/01/main-image-2.jpg' title='main-image-2'><img width="150" height="150" src="http://gamebox.la/wp-content/uploads/2012/01/main-image-2-150x150.jpg" class="attachment-thumbnail" alt="main-image-2" title="main-image-2" /></a>

Without:

<a href='http://site.com/wp-content/uploads/2012/01/main-image-2.jpg' title='main-image-2' rel="lightbox[170]"><img width="150" height="150" src="http://gamebox.la/wp-content/uploads/2012/01/main-image-2-150x150.jpg" class="attachment-thumbnail" alt="main-image-2" title="main-image-2" /></a>

It seems the filter does something to the post_gallery that makes WP not show the Lightbox part (missing rel=”lightbox[]”), but I can’t figure out why it does that.

Reference code in functions.php from here:

function exclude_thumbnail_from_gallery($null, $attr)
{
    if (!$thumbnail_ID = get_post_thumbnail_id())
        return $null; // no point carrying on if no thumbnail ID

    // temporarily remove the filter, otherwise endless loop!
    remove_filter('post_gallery', 'exclude_thumbnail_from_gallery');

    // pop in our excluded thumbnail
    if (!isset($attr['exclude']) || empty($attr['exclude']))
        $attr['exclude'] = array($thumbnail_ID);
    elseif (is_array($attr['exclude']))
        $attr['exclude'][] = $thumbnail_ID;

    // now manually invoke the shortcode handler
    $gallery = gallery_shortcode($attr);

    // add the filter back
    add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);

    // return output to the calling instance of gallery_shortcode()
    return $gallery;
}
add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);

Thanks a lot in advance!

UPDATE: I found this http://core.trac.wordpress.org/ticket/14130 it says it adds filter post_gallery_output but I can’t figure out what is that I have to modify in the current code to try another solution.

Related posts

Leave a Reply

1 comment

  1. I solved this by using this code

    function exclude_thumbnail_from_gallery($null, $attr)
    {
        if (!$thumbnail_ID = get_post_thumbnail_id())
            return $null; // no point carrying on if no thumbnail ID
    
        // temporarily remove the filter, otherwise endless loop!
        remove_filter('post_gallery', 'exclude_thumbnail_from_gallery');
    
        // pop in our excluded thumbnail
        if (!isset($attr['exclude']) || empty($attr['exclude']))
            $attr['exclude'] = array($thumbnail_ID);
        elseif (is_array($attr['exclude']))
            $attr['exclude'][] = $thumbnail_ID;
    
        // now manually invoke the shortcode handler
        $gallery = gallery_shortcode($attr);
    
        // add the filter back
        add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
    
        // return output to the calling instance of gallery_shortcode()
        return $gallery;
    }
    add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
    

    Then uninstalling the Lightbox for Galleries plugin and then installing the jQuery Lightbox For Native Galleries.

    Thank you all for your suggestions and help! 🙂