I want to extract images from the_content() and want to display first images from post on index.php and want to display all images on single.php. To do this I apply a filter on the_content():
<?php
/*****************************************************************************
Post Summary Filter - Grap Image Thumbnails, Strip Tags, etc
******************************************************************************/
add_filter('the_content', 'filterContentSummary', 0);
function filterContentSummary($content){
global $post;
if( is_home() || is_category() || is_tag() || is_author() || is_date() || is_search() || is_single ){
//if NOT single page or "page" page
$img=''; //default img code
$final_width = 300;
$content = $post->post_content;
//search for first image in article
preg_match('/<img[^>]*>/i',$content,$matches);
//image found, process it
if($matches){
preg_match('/src="[^"]*"/i',$matches[0],$src);
preg_match('/width="(\d*)[^"]*"/i',$matches[0],$width);
if($width[1] < $final_width){
$final_width = $width[1];
}
}
//prepare text
if(!empty($post->post_excerpt)){
$content = $post->post_excerpt;
}else{
//strip shortcodes, tags
$content = preg_replace('/\[\/*[^\]]*\]/i', '', $content); //cont is ready
$content = preg_replace('/<[^>]*>/i', '', $content); //cont is ready
$content = substr($content,0,500);
$content = explode(". ", $content);
array_pop($content);
$content = implode(". ", $content) . "";
}
if($content=="."){
$content = $post->post_content;
}
//prepare final content
$content = "<p>". $content ."</p>";
//Adding Read more link
$content .= "<p align='right' class='seemore'><a href=". get_permalink() . "></a></p>";
}
// Make sure to return the content
return $content;
}
?>
On index.php using regular expressions I find the first image from the post and display it:
<?php while (have_posts()) : the_post();
$content = $post->post_content;
preg_match_all('/(<img.*src="(.*)"[^>]*>)/iU',$content,$matches);
?>
Now $matches
have all the images:
<div class="content-img"><img src="<?php echo $matches[2][0];?>"/></div>
...used to display first image on a post.
But how I can display all images on single.php ?