Community Forums › Forums › Archived Forums › Design Tips and Tricks › Grab first image from post if no featured image
Tagged: featured image, grab image, posts
- This topic has 6 replies, 2 voices, and was last updated 9 years, 1 month ago by
amandathewebdev.
-
AuthorPosts
-
December 17, 2015 at 9:50 am #174079
amandathewebdev
MemberHi 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:
http://dentaluxpa.com/blog//*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; }
December 17, 2015 at 11:59 am #174092carrieoke13
ParticipantThis 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 #174093amandathewebdev
MemberThanks 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 #174107carrieoke13
Participanthmm. have you tried a plugin like Regenerate Thumbnails to see if it would fix the issue?
December 21, 2015 at 3:51 pm #174512amandathewebdev
MemberThanks for the suggestion. Unfortunately it didn't work.
December 21, 2015 at 4:17 pm #174516amandathewebdev
MemberIf 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 #174518amandathewebdev
MemberOK 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(); }
-
AuthorPosts
- The topic ‘Grab first image from post if no featured image’ is closed to new replies.