从图库代码中排除特色图像会使Lightbox停止工作

时间:2012-01-18 作者:Alekz

我正在寻找一种解决方案,以排除每篇文章中的特色图像/缩略图,因此我发现this great piece of code 并将其添加到函数中。php。它工作得很好,除了一件事:它使Lightbox jquery插件变得无用。

我在代码中看到的区别是这样的。使用代码:

<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>
无:

<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>
过滤器似乎对post\\u gallery做了一些事情,使WP不显示Lightbox部分(missing rel=“Lightbox[]”),但我不明白为什么它会这样做。

函数中的参考代码。php发件人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);
提前多谢!

UPDATE: 我找到了这个http://core.trac.wordpress.org/ticket/14130 它说它添加了filter post\\u gallery\\u输出,但我不知道为了尝试其他解决方案,我必须在当前代码中修改哪些内容。

1 个回复
最合适的回答,由SO网友:Alekz 整理而成

我用这个代码解决了这个问题

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);
然后卸载Lightbox for Galleries 插件,然后安装jQuery Lightbox For Native Galleries.

谢谢大家的建议和帮助!:)

结束

相关推荐

Pull images from the gallery

我正在构建一个模板,wordpress主要用作图像CMS,我从每篇文章的内容部分提取第一幅图像。<img src=\"<?php echo grab_image() ?>\" /> function grab_image() { global $post, $posts; $first_img = \'\'; ob_start(); ob_end_clean(); $output = preg_match_all(\'/&l