• Resolved muza11

    (@muza11)


    So I found a lot of tutorials with codes to do this. but at a point they said to put this code in the Loop. For Hestia Theme, where should i put this code?

    <?php
    // This code must should be inside WordPress post loop
    if ( has_post_thumbnail() ) {
    the_post_thumbnail();
    }
    else {
    echo get_first_image();
    }
    ?>

    Or any kind of help in this topic would be appreciated.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter muza11

    (@muza11)

    i used only this code in function.php at everything is fine. problem solved.

    function prefix_auto_featured_image() {
        global $post;
        if (!has_post_thumbnail($post->ID)) {
            $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', 'prefix_auto_featured_image');
    add_action('save_post', 'prefix_auto_featured_image');
    add_action('draft_to_publish', 'prefix_auto_featured_image');
    add_action('new_to_publish', 'prefix_auto_featured_image');
    add_action('pending_to_publish', 'prefix_auto_featured_image');
    add_action('future_to_publish', 'prefix_auto_featured_image');

    Hello @muza11!

    We are glad to hear the issue was solved in the meantime!

    Thanks for using Hestia and for reaching out!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘set the first image in the post as Featured Image’ is closed to new replies.