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;
}

About the author

Hamid Awan

Hamid Awan is a computer Geek, Internet Entrepreneur, Business Guy, Day Dreamer, Fitness Freak, Music Lover, Blogger and Digital Marketing Specialist. He also helps companies to grow their Online Business. At Tech Entires he writes about Money Making Ideas, Business, Android Apps, Blogging, WordPress, SEO, Technology and Computer Tips and Tricks. If you have any suggestion, you can contact us through our Contact Us Page

Leave a Comment