Exclude the_post_thumbnail from gallery shortcode

I use this code to create a simple gallery on the page:

<?php echo do_shortcode('[gallery itemtag="ul" icontag="li" size="full" columns="0" link="file" ]'); ?> 

The problem now is that the end user must upload the image through the Media page before selecting this image as a recognized image.

I know that this can be solved by adding an image identifier to the shortcode exception list, but how to get this identifier automatically?

+7
wordpress wordpress-theming
source share
3 answers
 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); 
+15
source share
 <?php $id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID ?> <?php echo do_shortcode('[gallery exclude='.$id.' link="file" itemtag="div" icontag="span" captiontag="p" size="thumbnail" columns="4" ]'); ?> 
+3
source share

What about?

 echo do_shortcode('[gallery exclude="' . get_post_thumbnail_id( $post->ID ) . '"]'); 
+1
source share

All Articles