Skip to content

Instantly share code, notes, and snippets.

@aaroncampbell
Created July 20, 2012 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaroncampbell/3148808 to your computer and use it in GitHub Desktop.
Save aaroncampbell/3148808 to your computer and use it in GitHub Desktop.
<?php
/**
* If the post doesn't have a featured image set it to the first attached image
* or a default if there are no images
*/
function pd_set_post_thumbnail( $post_id = null ) {
if ( ! isset( $post_id ) )
$post_id = get_the_ID();
if ( has_post_thumbnail( $post_id ) )
return true;
$attachments_args = array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'numberposts' => 1,
'post_mime_type' => 'image',
);
$default_args = array(
'name' => 'pandodaily-default',
'post_type' => 'attachment',
'numberposts' => 1,
'post_mime_type' => 'image',
);
if( ! $images = get_children( $attachments_args ) )
$images = get_posts( $default_args );
$image = array_pop( $images );
return set_post_thumbnail( $post_id, $image->ID );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment