Created
April 1, 2014 15:42
-
-
Save ingozoell/9916769 to your computer and use it in GitHub Desktop.
How to Set the First Post Image as a Featured Image (or Post thumbnail) Automatically
From http://beginnersbook.com/2013/09/set-default-thumbnail-featured-fallback-image-wordpress-automatically/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function autogen_featured_img() { | |
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); | |
} | |
} | |
} | |
} | |
/*This line is used to generate featured images for all old | |
posts. Remove this once the default images get generated | |
for all of the old posts*/ | |
add_action('the_post', 'autogen_featured_img'); | |
/* For new upcoming posts, leave them permanently*/ | |
add_action('save_post', 'autogen_featured_img'); | |
add_action('draft_to_publish', 'autogen_featured_img'); | |
add_action('new_to_publish', 'autogen_featured_img'); | |
add_action('pending_to_publish', 'autogen_featured_img'); | |
add_action('future_to_publish', 'autogen_featured_img'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment