CMS Scripts

As set out in the first image loaded in a post

 

This function automatically sets the first image loaded in an article as an image in evidence. Particularly useful when changing the theme and the same does not support it. As set out in the first image loaded in a post
File to edit: functions.php

0
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
add_filter(‘the_content’, ‘set_featured_image_from_attachment’);
function set_featured_image_from_attachment($content) {
global $post;
if (has_post_thumbnail()) {
// display the featured image
$content = the_post_thumbnail() . $content;
} else {
// get & set the featured image
$attachments = get_children(array(
‘post_parent’ => $post->ID,
‘post_status’ => ‘inherit’,
‘post_type’ => ‘attachment’,
‘post_mime_type’ => ‘image’,
‘order’ => ‘ASC’,
‘orderby’ => ‘menu_order’
));
if ($attachments) {
foreach ($attachments as $attachment) {
set_post_thumbnail($post->ID, $attachment->ID);
break;
}
// display the featured image
$content = the_post_thumbnail() . $content;
}
}
return $content;
}

Leave a Reply