Skip to content

Instantly share code, notes, and snippets.

@maor
Last active June 3, 2016 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save maor/3c65c8741cc41638281c to your computer and use it in GitHub Desktop.
Save maor/3c65c8741cc41638281c to your computer and use it in GitHub Desktop.
A theme helper function for Photon (http://developer.wordpress.com/docs/photon/)
<?php
function ch_photonize( $url = '', $params = array() ) {
$server_pool = rand( 0,3 );
return add_query_arg(
$params,
"https://i{$server_pool}.wp.com/" . str_replace( array( 'http://', 'https://' ), '', $url )
);
}
<?php
// Sample usage: (in a theme, within the loop)
// BTW: I know there are better ways to go about it, but this is just for demonstration purposes.
while ( have_posts() ) : the_post(); ?>
<div class="post-featured-image">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); ?>
<img src="<?php echo ch_photonize( current($image), array( 'crop' => '300,300' ) ); ?>" alt="">
<?php endif; ?>
</div>
<?php endwhile; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment