Exclude featured image from gallery in wp-admin

Is there a way to prevent the featured image from even showing up in a post’s gallery?

e.g. when editing the gallery in the admin for a post, the featured image isn’t there.

Related posts

Leave a Reply

3 comments

  1. Okay, I had the same problem and just solved it by adding an “exclude” field to the shortcode. Like so…

    [gallery link="file" columns="4" orderby="title" exclude="1050"]
    

    You have to just pull the ID number of the file you don’t want to show. This works without adding any functions or edits to the theme.

  2. I am agree with @janw’s answer.

    I would like to add further as:

    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 ?>
    
  3. If you use the gallery shortcode you can add the exclude param with the id of the featured image to exclude it.

    If you want to do it dynamically I suggest using the post_gallery filter.

    <?php // add to functions.php
    add_filter ('post_galley', 'exclude_featured_img');
    function exclude_featured_img($attr)
    {
        if (!isset($attr['exclude']) || empty($attr['exclude'])) {
            return $attr['exclude'] = get_post_thumbnail_id();
        } else {
            return $attr['exclude'] = $attr['exclude'] . ',' . get_post_thumbnail_id();
        }
    }
    

    not tested but should work.