-
-
Save GaryJones/9d38674c60a8e7319cc0 to your computer and use it in GitHub Desktop.
This file contains 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
<?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>'; | |
} |
Thanks. I've updated mine with (tested) code. https://gist.github.com/gregrickaby/dbc224eabbf3707d8c5d
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
else
, using guard clauses to return early.;
.alt
attributes required for valid markup.Untested.