Site logo

Archived topic

No feature image uploaded

15 replies · Started by Edin on January 8, 2018

Viewing posts 1–15 of 16

I have lots of older posts where I haven't defined any "featured image". As a result, I don't have images in the homepage articles view, on related posts, etc. with these articles.

Is it somehow possible to "pull" the first image of the post and define it as a "featured image"?

Thank you in advance!

Hi there,

That would require a bit of custom coding I think.

Might be worth asking a forum like this: https://stackoverflow.com/

Quick question to understand this better and to find a solution:

The images that are taken from the "featured image" and posted on the homepage articles view and on related posts, are called "WordPress Post Thumbnails"? Right?

Yeah I believe so.

Okay, then I believe that I've found a solution, I just don't know how to apply it.

Can you please give me a tip and tell me if this is a valid method?

I'm supposed to put this into my functions.php

//function to call first uploaded image in functions file
function main_image() {
$files = get_children('post_parent='.get_the_ID().'&post_type=attachment
&post_mime_type=image&order=desc');
  if($files) :
    $keys = array_reverse(array_keys($files));
    $j=0;
    $num = $keys[$j];
    $image=wp_get_attachment_image($num, 'large', true);
    $imagepieces = explode('"', $image);
    $imagepath = $imagepieces[1];
    $main=wp_get_attachment_url($num);
        $template=get_template_directory();
        $the_title=get_the_title();
    print "<img src='$main' alt='$the_title' class='frame' />";
  endif;
}

And here's what I don't know HOW to do:

The code above simply outputs the first image added to an article. Now we need to display this output in your theme. To do that, you will need to edit the theme files where post_thumbnail(); function is used. Replace it with the following code.

<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) {
  echo get_the_post_thumbnail($post->ID);
} else {
   echo main_image();
} ?>

Where do I put this code? And would you say that this works? (Don't want to break anything, or slow the site down...)

Thank you!

Thanks Tom, I tried it and it works. Problem is, it doesn't take the FIRST image and turns it into "feature" but another, which makes it look weird sometimes.

Any quick fix to the code to change this?

add_filter('get_post_metadata', function($value, $object_id, $meta_key, $single) {
	if ($meta_key !== '_thumbnail_id' || $value) {
		return $value;
	}

	preg_match('~<img[^>]+wp-image-(\\d+)~', get_post_field('post_content', $object_id), $matches);
	if ($matches) {
		return $matches[1];
	}
	return $value;
}, 10, 4);

Thanks again!

And on posts after the 2nd half of Page 2 and onwards, it doesn't work at all!

That's weird...

I can't really find any alternative methods - did you have any luck posting on stackexchange?

Tom, I think I'm going to do it the old-fashioned way and go through every article and do it manually. It's 200+ articles, so this can take a while...

I don't believe that's possible unfortunately.

Tom, I've just learned that this plugin you've mentioned "quick featured images" offers a way to quickly change the images in the post-list view, WITHOUT changing the date.

So awesome!

Thanks again for the help!

Edin

This archived topic is closed to new replies.