Welcome to GSM VIP. GSM Support Forum!

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features.
Only registered members may post questions, contact other members, search our boards. Registration is fast, simple and absolutely free so please - Click to Register or Login !

Automatically Set the Featured Image on Wordpress

Here in all blogging tips and others internet stuff...
Post Reply
Posts: 11
Joined: Fri Dec 14, 2018 2:24 pm

Automatically Set the Featured Image on Wordpress

Post by ...:::Cain...::: » Tue Jan 01, 2019 11:54 pm

Sometimes you don’t have a featured image for a post and you want the first image of your article to be used as the featured image automatically.

Instructions: Add the following code to the functions.php file of your WordPress theme to automatically set the featured image for your posts. If you add a featured image to a post, then it will override this code and display your featured image instead.

Code: [ Please, Login to view this link]

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');

Posts: 3
Joined: Sat Jan 26, 2019 12:19 am
Location: Denmark

Automatically Set the Featured Image on Wordpress

Post by TimothySeime » Sun Mar 10, 2019 7:07 am

Anyone know where I can get a free wordpress theme that is already SEOd but looks fairly good so I can use it for a flagship?

Post Reply