Return to Snippet

Revision: 17322
at September 1, 2009 03:19 by crs


Updated Code
<?php
// ADD THIS IN THE functions.php FILE

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/uploads/default.jpg";
  }
  return $first_img;
}?>

---------------------------------------------------------------------

<?php

// ADD THIS IN THE DESIRED TEMPLATE FILE

	$content = $post->post_content;
	$searchimages = '~<img [^>]* />~';

	/*Run preg_match_all to grab all the images and save the results in $pics*/

	preg_match_all( $searchimages, $content, $pics );

	// Check to see if we have at least 1 image
	$iNumberOfPics = count($pics[0]);

	if ( $iNumberOfPics > 0 ) { ?>
        <!-- GRAB THE IMAGE AND USE WHATEVER HTML YOU LIKE -->
          <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a>
	<?php }

?>

Revision: 17321
at September 1, 2009 03:19 by crs


Updated Code
<?php
// ADD THIS IN THE functions.php FILE

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "http://ypotrofies.gr/v3/uploads/default.jpg";
  }
  return $first_img;
}?>

---------------------------------------------------------------------

<?php

// ADD THIS IN THE DESIRED TEMPLATE FILE

	$content = $post->post_content;
	$searchimages = '~<img [^>]* />~';

	/*Run preg_match_all to grab all the images and save the results in $pics*/

	preg_match_all( $searchimages, $content, $pics );

	// Check to see if we have at least 1 image
	$iNumberOfPics = count($pics[0]);

	if ( $iNumberOfPics > 0 ) { ?>
        <!-- GRAB THE IMAGE AND USE WHATEVER HTML YOU LIKE -->
          <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a>
	<?php }

?>

Revision: 17320
at September 1, 2009 03:18 by crs


Updated Code
<?php
// ADD THIS IN THE functions.php FILE

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "http://ypotrofies.gr/v3/uploads/default.jpg";
  }
  return $first_img;
}?>

---------------------------------------------------------------------

<?php

// ADD THIS IN THE DESIRED TEMPLATE FILE

	$content = $post->post_content;
	$searchimages = '~<img [^>]* />~';

	/*Run preg_match_all to grab all the images and save the results in $pics*/

	preg_match_all( $searchimages, $content, $pics );

	// Check to see if we have at least 1 image
	$iNumberOfPics = count($pics[0]);

	if ( $iNumberOfPics > 0 ) { ?>
        // GRAB THE IMAGE AND USE WHATEVER HTML YOU LIKE
          <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a>
	<?php }

?>

Revision: 17319
at September 1, 2009 03:18 by crs


Initial Code
<?php
// ADD THIS IN THE functions.php FILE

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "http://ypotrofies.gr/v3/uploads/default.jpg";
  }
  return $first_img;
}?>

---------------------------------------------------------------------

<?php
	$content = $post->post_content;
	$searchimages = '~<img [^>]* />~';

	/*Run preg_match_all to grab all the images and save the results in $pics*/

	preg_match_all( $searchimages, $content, $pics );

	// Check to see if we have at least 1 image
	$iNumberOfPics = count($pics[0]);

	if ( $iNumberOfPics > 0 ) { ?>
        // GRAB THE IMAGE AND USE WHATEVER HTML YOU LIKE
          <img src="<?php echo catch_that_image() ?>" alt="<?php the_title(); ?>" /></a>
	<?php }

?>

Initial URL


Initial Description


Initial Title
Check if the post has at least one image and display it

Initial Tags
image, wordpress

Initial Language
PHP