Advertisement
alchymyth

featured image in post - shortcode

Oct 14th, 2012
3,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.35 KB | None | 0 0
  1. //2012 oct alchymyth @ transformationpowertools.com
  2. //shortcode to insert post_thumbnail into content
  3. //usage [thumbnail size=medium align=left]
  4.  
  5. add_shortcode('thumbnail','insert_post_thumbnail_into_content');
  6.  
  7. function  insert_post_thumbnail_into_content( $atts ) {
  8.    extract( shortcode_atts( array(
  9.       'size' => 'post-thumbnail', // any of the possible post thumbnail sizes - defaults to 'thumbnail'
  10.       'align' => 'none' // any of the alignments 'left', 'right', 'center', 'none' - defaults to 'none'
  11.       ), $atts ) );
  12.     global $post;
  13.    
  14.     if( ! get_post_thumbnail_id( $post->ID ) ) return false; //no thumbnail found
  15.    
  16. //alignment check
  17.     if( !in_array( $align, array( 'left', 'right', 'center', 'none' ) ) ) $align = 'none';
  18.     $align = 'align' . $align;
  19.    
  20. //thumbnail size check
  21.     if( !(preg_match( '|array\((([ 0-9])+,([ 0-9])+)\)|', $size ) === 1) && !in_array( $size, get_intermediate_image_sizes() ) ) $size = 'post-thumbnail';
  22.     if( preg_match( '|array\((([ 0-9])+,([ 0-9])+)\)|', $size, $match ) === 1 ) $sizewh = explode( ',', $match[1] ); $size = array( trim( $sizewh[0] ), trim( $sizewh[1] ) );
  23.    
  24. //get the post thumbnail
  25.     $thumbnail = get_the_post_thumbnail( $post->ID, $size );
  26.  
  27. //integrate the alignment class
  28.     $thumbnail = str_replace( 'class="', 'class="' . $align . ' ', $thumbnail ); //add alignment class
  29.  
  30. return $thumbnail;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement