Replies: 0
I would like to be able to use the first Image in an article as the post thumbnail if there is no featured image defined
I came across an article which suggests adding the following to 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 add the following to the theme files where <strong>post_thumbnail();</strong> 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();
} ?>
my questions
1. would that work with oceamwp?
2. Would it really use the first Image in an article ONLY if there is no featured image defined or would it replace feature image on all posts?
3. Where can I find the file containing this? post_thumbnail(); function is used.
look forward to hearing your thoughts