Insert thumbnails automatically

The thumbnails in WordPress are used again and again in the overview, because the small pictures reveal a lot about the content. They are especially important because users can establish direct connections and view content without having to read long texts or descriptions. So they are very useful. But thumbnails are often forgotten, sometimes this is not even noticed under stress. A snippet provides security, because it automatically uses the first picture in the mail as a thumbnail. So whenever you forget to choose a thumbnail, WordPress automatically uses the first picture in your article.

Copy the following into the Functions.php of your theme:

function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb) {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');

Protection for thumbnails

The snippet at the top is ideal for protection. If it has happened to you several times that you have not assigned a thumbnail to an article, then you should definitely install the snippet above. Since pictures in articles are standard in the meantime, you should always find a corresponding graphic here. This way the display in the archive will always be correct, even if no image has been assigned manually. Very useful, especially when articles are often written in stress or even on the road. Alternatively, you can use another snippet, which uses a standard image as thumbnailif no special image has been assigned in the article.

About Christian

My name is Christian and I am co-founder of the platform fastWP. Here in the magazine I am responsible for the more "technical" topics but I like to write about SEO, which has been my passion for over 10 years now.

1 thought on “Erstes Bild im Post automatisch als Thumbnail verwenden”

  1. Hello, Christian,
    ...the postal service may be a stone age... But it's the only one in TOTAL Google that helped. All others are based on the hard reconstruction of the links.
    I still have a few posts (out of 18,000) that are not running in the Stacker Lite and Koji theme.
    Are you - against good payment of course - willing to help out a little?

Leave a Comment

Your email address will not be published. Required fields are marked *