wordpress - Exclude the_post_thumbnail from gallery shortcode -
i using code have simple gallery on page:
<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?> the problem end-user has upload image via media page before selecting image featured image.
i know solved adding featured image's id shortcode's exclude list, how id automatically?
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 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; // manually invoke shortcode handler $gallery = gallery_shortcode($attr); // add filter add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2); // return output calling instance of gallery_shortcode() return $gallery; } add_filter('post_gallery', 'exclude_thumbnail_from_gallery', 10, 2);
Comments
Post a Comment