Created
July 1, 2012 15:40
-
-
Save ekka21/3028773 to your computer and use it in GitHub Desktop.
Wordpress: saving default post thumbnail
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
//Paste this in function.php | |
add_action( 'save_post', 'wptuts_save_thumbnail' ); | |
function wptuts_save_thumbnail( $post_id ) { | |
// Get Thumbnail | |
$post_thumbnail = get_post_meta( $post_id, $key = '_thumbnail_id', $single = true ); | |
// Verify that post is not a revision | |
if ( !wp_is_post_revision( $post_id ) ) { | |
// Check if Thumbnail exists | |
if ( empty( $post_thumbnail ) ) { | |
// Add thumbnail to post | |
update_post_meta( $post_id, $meta_key = '_thumbnail_id', $meta_value = 'ATTATCHMENT_ID' ); | |
} | |
} | |
} | |
//in the loop | |
if (has_post_thumbnail()) { | |
the_post_thumbnail(); | |
} | |
else { | |
echo '<img src="' . get_bloginfo('template_directory') . '/images/thumb-default.png' . '" width="100" height="100" alt="thumbnail" />'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment