• Skip to main content
  • Skip to forum navigation

StudioPress

  • Shop for Themes
  • My StudioPress

Forum navigation

  • Home
  • General Genesis Discussions
  • StudioPress Themes
  • Genesis Blocks
    • Genesis Blocks
    • Genesis Custom Blocks
  • Retired Themes
  • FAQs
  • Forum Rules
  • Internationalization and Translations
  • Forum Bugs and Suggestions
  • Forum Log In

Are You Using The WordPress Block Editor?

Genesis now offers plugins that help you build better sites faster with the WordPress block editor (Gutenberg). Try the feature-rich free versions of each plugin for yourself!

Genesis Blocks Genesis Custom Blocks

Grab first image from post if no featured image

Welcome!

These forums are for general discussion on WordPress and Genesis. Official support for StudioPress themes is offered exclusively at My StudioPress. Responses in this forum are not guaranteed. Please note that this forum will require a new username, separate from the one used for My.StudioPress.

Log In
Register Lost Password

Community Forums › Forums › Archived Forums › Design Tips and Tricks › Grab first image from post if no featured image

This topic is: resolved

Tagged: featured image, grab image, posts

  • This topic has 6 replies, 2 voices, and was last updated 9 years, 10 months ago by amandathewebdev.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • December 17, 2015 at 9:50 am #174079
    amandathewebdev
    Member

    Hi all,

    I recently migrated a whole bunch of old blog posts to a new site and apparently WP Importer doesn't grab images. Anywho the problem is it would be way too time consuming to go through all 100+ posts and set a featured image. Is there a way to grab whatever the first image is and display it as a featured image?

    This code worked before I imported the posts, but no longer does:

    /*look for featured image, if not, grab post image*/
    function get_image() {
      if ( has_post_thumbnail() ) {
    	$src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumb' );
    	$fbimage = $src[0];
      } else {
    	global $post, $posts;
    	$fbimage = '';
    	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',
    	$post->post_content, $matches);
    	$fbimage = $matches [1] [0];
      }
      if(empty($fbimage)) {
        $fbimage = site_url().'/wp-content/themes/child/images/logo_transparent.png';
      }
      return $fbimage;
    }
    http://dentaluxpa.com/blog/
    December 17, 2015 at 11:59 am #174092
    carrieoke13
    Participant

    This is the code I used and it used to work with Minimum Pro - it's pretty similar to yours:

    //*get first image
    
    function catch_that_image() {
      global $post, $posts;
      $first_img = '';
      ob_start();
      ob_end_clean();
      $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
      $first_img = $matches[1][0];
    
      if(empty($first_img)) {
        $first_img = "/path/to/default.png";
      }
      return $first_img;
    }
    

    This tutorial also looks promising: http://www.wpbeginner.com/wp-themes/how-to-set-a-default-fallback-image-for-wordpress-post-thumbnails/

    December 17, 2015 at 12:05 pm #174093
    amandathewebdev
    Member

    Thanks for the reply carrieoke13. That code threw an error for me though.

    My code is working but not on the posts I migrated. I think the issue might be that the images were not uploaded into THIS particular WP install. Even though the folders exist under wp-content > uploads with the images inside, they are not a part of the media library. The images I upload to the media library pull correctly if no featured image.

    Why is that the case? Very weird. So maybe I should treat these images as external linked images? How would I pull those?

    December 17, 2015 at 1:50 pm #174107
    carrieoke13
    Participant

    hmm. have you tried a plugin like Regenerate Thumbnails to see if it would fix the issue?

    December 21, 2015 at 3:51 pm #174512
    amandathewebdev
    Member

    Thanks for the suggestion. Unfortunately it didn't work.

    December 21, 2015 at 4:17 pm #174516
    amandathewebdev
    Member

    If I add add_action( 'genesis_get_image', 'get_image', 3 ); then I see the img URL text next to every blog post. Not an image, but it's something.

    December 21, 2015 at 4:39 pm #174518
    amandathewebdev
    Member

    OK I figured it out with (a lot of) help from a co-worker. If anyone stumbles across this, this is what I did, all in my functions.php:

    function get_src() {
      if ( has_post_thumbnail() ) {
    	$src = wp_get_attachment_image_src( get_post_thumbnail_id(), 'thumb' );
    	$fbimage = $src[0];
      } else {
    	global $post, $posts;
    	$fbimage = '';
    	$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',
    	$post->post_content, $matches);
    	$fbimage = $matches [1] [0];
      }
      if(empty($fbimage)) {
        $fbimage = site_url().'/wp-content/themes/epik/img/logo.png';
      }
      return $fbimage;
    }
    add_filter('genesis_get_image', 'default_image_fallback', 10, 2);
    function default_image_fallback($output, $args) {
        return get_image();
    }
    
    function get_image($class="") {
        $src = get_src();
        ob_start()?>
        <a href="<?php echo get_permalink() ?>">
            <img class="featured-image <?php echo $class ?>" src="<?php echo $src ?>" alt="<?php echo get_the_title() ?>" />
        </a>
        <?php return ob_get_clean();
    }
  • Author
    Posts
Viewing 7 posts - 1 through 7 (of 7 total)
  • The topic ‘Grab first image from post if no featured image’ is closed to new replies.

CTA

Ready to get started? Create a site or shop for themes.

Create a site with WP EngineShop for Themes

Footer

StudioPress

© 2025 WPEngine, Inc.

Products
  • Create a Site with WP Engine
  • Shop for Themes
  • Theme Features
  • Get Started
  • Showcase
Company
  • Brand Assets
  • Terms of Service
  • Accptable Usse Policy
  • Privacy Policy
  • Refund Policy
  • Contact Us
Community
  • Find Developers
  • Forums
  • Facebook Group
  • #GenesisWP
  • Showcase
Resources
  • StudioPress Blog
  • Help & Documentation
  • FAQs
  • Code Snippets
  • Affiliates
Connect
  • StudioPress Live
  • StudioPress FM
  • Facebook
  • Twitter
  • Dribbble