Exclude the_post_thumbnail from gallery shortcode

后端 未结 3 1072
北海茫月
北海茫月 2020-12-29 14:56

I am using this code to have a simple gallery on the page:



        
3条回答
  •  盖世英雄少女心
    2020-12-29 15:18

    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);
    

提交回复
热议问题