1

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:

<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.

2
  • Which plugin do you use for lightbox? If it works not with the same filter you use but somehow before that, it might be that you overwrite its effect.
    – Raphael
    Commented Jan 18, 2012 at 10:38
  • I tried two different plugins, the result is the same, yes it's that the filter overwrites the plugin effect. I'm still trying to find a way for the filter not to interfere :(
    – Alekz
    Commented Feb 8, 2012 at 22:28

1 Answer 1

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! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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