Remove Post Thumbnail from the Gallery Shortcode (7 posts)

  1. magnusc
    Posted 4 years ago #

    I have activated the post thumbnail function (in WordPress 2.9) and it works fine, but

    when I get my post gallery with the gallery_shortcode i also get the thumbnail as part of my gallery. Is there a way to exclude the thumbnail from the gallery? Something like:
    [gallery exclude"post-thumbnail"]

  2. esmi
    Forum Moderator
    Posted 4 years ago #

  3. magnusc
    Posted 4 years ago #

    Hi esmi
    What am I excluding when I exclude 21? Where can I set all my thumbnail to automaticly have ID=21?

    All my posts will have a gallery with images, and one of these images will be the post thumbnail. I do not want this thumbnail to show up in my slideshow/gallery inside posts so how do I exclude it?

    The pic on the bottom on this page is the thumbnail.

  4. Catia Kitahara
    Posted 4 years ago #


    You can edit your single template to automatically show the gallery without needing to insert the gallery shortcode manually.
    Open your single.php file and inside the loop, where you want the gallery to be displayed, copy this line of code:

    <?php  $id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID ?>
    <?php echo do_shortcode("[gallery exclude={$id}]"); // insert the gallery without the thumbnail ?>
  5. crismdq
    Posted 4 years ago #

    THANKS Cátia, you're the best.

  6. erezp5
    Posted 4 years ago #

    same here. Cátia, you're the greatest.

  7. luminopolis
    Posted 3 years ago #

    You're a lifesaver Cátia.

    For anyone using get_posts or similar to manually loop through attachments, her solution still works. For example:

    	$thumb_id = get_post_thumbnail_id(get_the_ID()); // gets the post thumbnail ID
    	$args = array(
    		'order' => 'ASC',
    		'orderby' => 'menu_order',
    		'post_type' => 'attachment',
    		'post_parent' => $post->ID,
    		'post_mime_type' => 'image',
    		'post_status' => null,
    		'numberposts' => -1,
    		'exclude' => $thumb_id
    	$attachments = get_posts($args);
    	if ($attachments) {
    		foreach ($attachments as $attachment) {
    			echo wp_get_attachment_image($attachment->ID, 'medium', false);

Topic Closed

This topic has been closed to new replies.

About this Topic