Remove Duplicate Image Attachment In Post As Featured Image

Many of the new HTML 5 StudioPress child themes automatically display the first image from the content area as the featured image on all archive pages which can duplicate the image on single posts and archive pages.

Even if you don’t add a featured image to a post, Genesis pulls an image from the post and uses it as a featured image.

Here’s the code in genesis which makes this happen:

//* Check for post image (native WP)
    if ( has_post_thumbnail( $args['post_id'] ) && ( 0 === $args['num'] ) ) {
        $id = get_post_thumbnail_id( $args['post_id'] );
        $html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );
        list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );
    }
    //* Else if first-attached, pull the first (default) image attachment
    elseif ( 'first-attached' === $args['fallback'] ) {
        $id = genesis_get_image_id( $args['num'], $args['post_id'] );
        $html = wp_get_attachment_image( $id, $args['size'], false, $args['attr'] );
        list( $url ) = wp_get_attachment_image_src( $id, $args['size'], false, $args['attr'] );
    }

This is not such a bad thing really however some people prefer this didn’t happen which is why Genesis Developer Gary Jones has worked out one solution.

Update: I’ve also created another solution which you’ll find at the end of this post which checks if a featured image has been added and removes it from single posts if added.

Simply add this code in your child themes functions file and it will stop your theme from using any images from your content area as post thumbnails on your blog, category and other archive pages.

Note: This code (from Gary Jones) is only needed for sites running Genesis 2.0.

Remove this code || 'archive' !== $args['context'] to target single posts.

Note from Gary: The reason for the isset(), is that not all calls to genesis_get_image() may have an argument array that contains a context key, since it was only added in Genesis 2.0.

Alternative Solution

This PHP code once added to the end of your child themes functions file, prevents Genesis from pulling any attachment images from the post content area and displaying them as your featured image before the entry title or on any type of archive page.

The code only displays a featured image which you have set manually using the Featured Image box included on all Edit Post/Page screens.

Related Tutorials With Code


Comments

16 responses to “Remove Duplicate Image Attachment In Post As Featured Image”

  1. Hi Brad,
    Thanks for this code.

    For me the code didn’t work first but after dat I removed on line 5:
    return $args;

    It works fine.

    Regards,
    Mirjam

    1. Brad Dalton Avatar
      Brad Dalton

      Thanks for letting me know Mirjam.

      That snippet isn’t mine and was written by someone else.

      I did test the second snippet which i did write and it worked for me.

  2. Brad, thanks for putting this out there. Just want to make sure I’m going to use the correct thing…. I want to make sure the featured image goes to the home page, widget is set up….. But I dont want the featured image showing at the top of the page that I labeled it from. Not sure if this makes sense.

    I want to prevent the pic at the top of the page… But not on the home page…

    Thanks for your help.

    1. Brad Dalton Avatar
      Brad Dalton

      Hello Rib

      Please clarify.

      The featured image is generally shown on the archive pages like blog, category and home page.

      It can also be displayed on single posts depending on which version of Genesis you’re running and which child theme you use.

      Your theme is running the old XHTML markup and also includes an after header featured image.

      1. I added a page. Then I added a featured image to the page. After adding the featured image and updating the page…. The featured image was at the top of the page and where it was originally inserted.

        Locations of the featured image.
        I would like the featured image not to show up at the top of the page but only where it was originally placed. Hope this more clear.

        1. Brad Dalton Avatar
          Brad Dalton

          Where did you add the featured image? To the featured image meta box or within the content area of your editor?

          You will need to remove the code from your child themes functions.php file which displays the first image in a post before the single posts entry header(title).

          1. I added the featured image at the box on the right of the editor.
            I found where to remove the code in the functions.php…. Does it matter that I’m trying to remove it from a page? Not a post?
            [code]
            /** Add the featured image section */
            add_action( ‘genesis_after_header’, ‘minimum_featured_image’ );
            function minimum_featured_image() {
            if ( is_home() ) {
            echo ”;
            genesis_widget_area( ‘home-slider’, array( ‘before’ => ”, ) );
            echo ”;
            }
            elseif ( is_singular( array( ‘post’, ‘page’ ) ) && has_post_thumbnail() ){
            echo ”;
            echo get_the_post_thumbnail($thumbnail->ID, ‘header’);
            echo ”;
            }
            }

            Since i’ve already crashed a site or two messing with functions.php…. What lines should I remove?

            }
            elseif ( is_singular( array( ‘post’, ‘page’ ) ) && has_post_thumbnail() ){
            echo ”;
            echo get_the_post_thumbnail($thumbnail->ID, ‘header’);
            echo ”;
            }
            [/code]
            Looks like this is what I should remove.

          2. Brad Dalton Avatar
            Brad Dalton

            I cannot read that code. Please paste it into a Github Gist and embed it in a comment or link to it from here.

          3. below… It looks like these are the lines of code I need to remove?

          4. Brad Dalton Avatar
            Brad Dalton

            Which ones Rob? Your code may not display correctly unless its embedded correctly.

          5. Remove lines 8-13… Seems right to me but I dont know for sure.

  3. Hi brad,

    Please not missing closing parenthesis in
    if ( ! isset( $args[‘context’] || ‘archive’ !== $args[‘context’] )

  4. Christian Karasiewicz Avatar
    Christian Karasiewicz

    Brad,

    Great tip. So this will stop Genesis from selecting from any images in a post and force it to use the featured image, correct?

    If you have this code installed and no featured image is added to a post, what does it use instead?

    Is there a way to tweak this code to have it use a default image if there isn’t a featured image?

    1. Brad Dalton Avatar
      Brad Dalton

      Only if one is added.

      There’s other ways to do that.

      There’s a plugin or code yo can choose from.

      1. Christian Karasiewicz Avatar
        Christian Karasiewicz

        If I wanted to add the code for this, would there be a way to include it within the code suggestion above?

        If not, do you have an example of code to do this?


        Christian

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.