Support » Theme: OceanWP » Use First Image in an Article as Post Thumbnail if there is no featured image

  • 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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Try to add the below code to the functions.php file of the child theme and check it works or not –

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

    1. Yes, it will work if the code is added correctly
    2. It will work if there is no featured image is defined.
    3. OceanWP > Partials > Single > Media > blog.single.php file

    hi Amit
    Thank you very much for responding.
    I tried it. here is what happened:

    It defined the first image on the page AS THE FEATURED IMAGE. and displayed it in the header of the page.

    What I am after is: avoid using A FEATURED IMAGE on a particular category of posts – but I want the first image of the post to appear as the post thumbnail on the category archive page.

    I have FEATURED IMAGE IN PAGE HEADER selected – I would like to keep that and use it on some posts of other categories. love that!

    In the past I used the suffusion theme. It had the option to define what appears as thumbnail and in what order – say first featured image, if not defined, then first image of the page, and on and on…

    Is that possible to do?

    Hello,

    Unfortunately, we don’t have these features at present. If you need this feature, then you can hire a developer for it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Use First Image in an Article as Post Thumbnail if there is no featured image’ is closed to new replies.