Skip to content

Instantly share code, notes, and snippets.

@lmartins
Forked from GaryJones/functions.php
Last active August 29, 2015 14:10
Show Gist options
  • Save lmartins/dc5f9cce2f2863b35c00 to your computer and use it in GitHub Desktop.
Save lmartins/dc5f9cce2f2863b35c00 to your computer and use it in GitHub Desktop.
<?php
/**
* Get post image.
*/
function wds_get_post_image( $size = 'thumbnail' ) {
// If featured image is present, use that
if ( has_post_thumbnail() ) {
return get_the_post_thumbnail( $size );
}
// Check for any attached image
$media = get_attached_media( 'image', get_the_ID() );
// Set up default image path
$media_url = get_stylesheet_directory_uri() . '/imgs/default-post-thumbnail.png';
// If an image is present, then use it as a thumbnail
if ( is_array( $media ) && 0 < count( $media ) ) {
$media = current( $media );
$media_url = ( 'thumbnail' === $size ) ? wp_get_attachment_thumb_url( $media->ID ) : wp_get_attachment_url( $media->ID );
}
return '<a href="' . esc_url( get_the_permalink() ) . '"><img src="' . esc_url( $media_url ) . '" alt="" /></a>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment