Skip to content

Instantly share code, notes, and snippets.

@vilmosioo
Last active October 13, 2015 15:38
Show Gist options
  • Save vilmosioo/4218112 to your computer and use it in GitHub Desktop.
Save vilmosioo/4218112 to your computer and use it in GitHub Desktop.
A smarter way to get post thumbnail
<?php
function post_thumbdail($full){
if ( has_post_thumbnail() ) {
echo "<aside><a href='".get_permalink()."' title='".get_the_title()."' rel='canonical'>";
the_post_thumbnail($full);
echo "</a></aside>";
}else{
if( $full == 'full' ) return;
$attachments = get_posts( array(
'post_type' => 'attachment',
'numberposts' => 1,
'post_parent' => get_the_ID(),
'exclude' => get_post_thumbnail_id()
) );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$href = wp_get_attachment_image_src( $attachment->ID, $full);
}
echo "<aside><a class='default' href='".get_permalink()."' title='".get_the_title()."' rel='canonical'>";
echo "<img src='".$href[0]."' alt='".get_the_title()."'/>";
echo "</a></aside>";
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment