catchresponsive_get_first_image

Return the first image in a post.

Description

([string]) catchresponsive_get_first_image( ([integer]) $postID, ([string/array]) $size, ([string/array]) $attr ); 

Works inside a loop.

Returns ([string])

image html

Parameters (3)

0. $postid ([integer])
[Post or page id]
1. $size ([string/array])
Image size. Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32).
2. $attr ([string/array])
Query string or array of attributes.

Usage

  1. if ( !function_exists( 'catchresponsive_get_first_image' ) ) { 
  2. require_once '/inc/catchresponsive-core.php'; 
  3.  
  4. // [Post or page id] 
  5. $postid = null; 
  6.  
  7. // Image size. Either a string keyword (thumbnail, medium, large or full) or a 2-item array representing width and height in pixels, e.g. array(32,32). 
  8. $size = null; 
  9.  
  10. // Query string or array of attributes. 
  11. $attr = null; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = catchresponsive_get_first_image($postid, $size, $attr); 
  15.  

Defined (1)

The function is defined in the following location(s).

/inc/catchresponsive-core.php  
  1. function catchresponsive_get_first_image( $postID, $size, $attr ) { 
  2. ob_start(); 
  3.  
  4. ob_end_clean(); 
  5.  
  6. $image = ''; 
  7.  
  8. $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', get_post_field('post_content', $postID ) , $matches); 
  9.  
  10. if ( isset( $matches [1] [0] ) ) { 
  11. //Get first image 
  12. $first_img = $matches [1] [0]; 
  13.  
  14. return '<img class="pngfix wp-post-image" src="'. esc_url( $first_img ) .'">'; 
  15. else { 
  16. return false;