Last active
August 29, 2015 14:01
-
-
Save scofennell/1ce335ac687c76165261 to your computer and use it in GitHub Desktop.
WordPress function to
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 the current post featured image, with a call to wp_is_mobile to grab a smaller size for mobile users. | |
* | |
* This function uses wp_is_mobile(), which is not intended for public use on the front end -- buyer beware. | |
* @return string|bool Returns the src for the featured image, or false on failure | |
*/ | |
function sjf_deh_get_image_src(){ | |
// $post must work in order to grab the featured image | |
$post = get_post(); | |
if( empty( $post ) ) { return false; } | |
$post_id = absint( $post->ID ); | |
//we're trying to manipulate posts of the attachment type only | |
if ( empty( $post->post_type ) || ( $post->post_type != 'attachment' ) ) { return false; } | |
// mobile users get a smaller version of the image | |
// wp_is_mobile is not intended for use on the front end because it can go haywire if you use caching on your site. | |
if( wp_is_mobile() ) { | |
$retina_img = wp_get_attachment_image_src( $post_id, 'sjf_deh_mobile_bg' ); | |
} else { | |
$retina_img = wp_get_attachment_image_src( $post_id, 'sjf_deh_bg' ); | |
} | |
// if there was no featured image, bail | |
if( !$retina_img ) { return false; } | |
// sanitize and return the image src | |
$out = esc_url( $retina_img[0] ); | |
return $out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment