I have written the code in php for putting the image in article post in excerpts mode. Its executing only once and showing image for only first article. For the next article it shows error " Fatal error: Cannot redeclare tfe_get_image() (previously declared in /home/nitendra/public_html/explorenikaya.com/wp-content/themes/WP-Genius/img.php:16) in /home/nitendra/public_html/explorenikaya.com/wp-content/themes/WP-Genius/img.php on line 94" you can check http://explorenikaya.com Please find the solution for me why is it not re-declaring the tfe_get_image() again. Here is the code <?php if(!defined('WP_CONTENT_URL')) define('WP_CONTENT_URL', get_option('siteurl') . '/wp-content'); add_filter("the_excerpt","tfe_put_image"); add_filter("the_excerpt_rss","tfe_put_image"); if(function_exists('add_theme_support')) add_theme_support('post-thumbnails'); ?> <?php function tfe_get_image($id,$content,$align,$width,$height,$default,$regenerate,$default_src) { $first_img = ''; //extract image preg_match_all('~<img [^\>]*>~', $content, $matches); $first_image = $matches[0][0]; //extract alt preg_match_all('~alt=[\'"]([^\'"]+)[\'"]~', $first_image, $matches); $alt = $matches[1][0]; //extract title preg_match_all('~title=[\'"]([^\'"]+)[\'"]~', $first_image, $matches); $title = $matches[1][0]; //extract src preg_match_all('~src=[\'"]([^\'"]+)[\'"]~', $first_image, $matches); $src=$matches[1][0]; $srcorig=$matches[1][0]; //last try, for images with src without quotes if(empty($src)){ preg_match_all('~src=([^\'"]+) ~', $first_image, $matches); $src=$matches[1][0]; $srcorig=$matches[1][0]; } //find base image for jpg $imgtype=array("jpg","jpeg","gif","png"); foreach($imgtype as $type){ preg_match_all('~(\w*)-([0-9]*)x([0-9]*).('.$type.')~', $src, $matches); if(!empty($matches[1][0])) $src = substr($src,0,strrpos($src,'/')).'/'.$matches[1][0].'.'.$matches[4][0]; $src = str_replace(".".$type,"-".get_option("thumbnail_size_w")."x".get_option("thumbnail_size_h").".".$type,$src); } $noimg=false; if(empty($src)){ //use the default image $src = $default_src; $noimg=true; } if($default=='no' && $noimg==true){ //not to show default? ok! nothing to show! } else { if(stripos($src,WP_CONTENT_URL)!==false){ //the image is hosted on the blog $rest = str_replace(WP_CONTENT_URL,'',$src); if(!file_exists(realpath(".").'/wp-content'.$rest)) if($regenerate=="yes"){ //thumbnail not found and I'm allowed to regenerate $images =& get_children( array( 'post_parent' => $id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => -1, 'post_status' => 'inherit', 'post_parent' => null, // any parent 'output' => 'object', ) ); foreach($images as $image) $ids[] = $image->ID; require_once(ABSPATH .'/wp-includes/post.php'); require_once(ABSPATH .'/wp-admin/includes/image.php'); foreach($ids as $idimg){ $fullsizepath = get_attached_file($idimg); if(false === $fullsizepath || !file_exists($fullsizepath)) die('-1'); set_time_limit(60); wp_update_attachment_metadata( $idimg, wp_generate_attachment_metadata( $idimg, $fullsizepath)); } } if(!file_exists(realpath(".").'/wp-content'.$rest)) //thumbnail still not found: this means the image is below 150x150 //or is a problem with regeneration; anyway, we use the original image $src=$srcorig; } else //the image is hosted outside the blog! $src=$srcorig; if($src==$srcorig) return '<img width="'.$width.'" src="'.$src.'" class="align'.$align.' wp-post-image tfe" alt="'.$alt.'" title="'.$title.'" />'; else return '<img width="'.$width.'" height="'.$height.'" src="'.$src.'" class="align'.$align.' wp-post-image tfe" alt="'.$alt.'" title="'.$title.'" />'; } } function tfe_put_image($excerpt){ if(is_single()) return; global $wp_query, $wpdb; $id = $wp_query->post->ID; //get the options you've choose $content = $wpdb->get_var('select post_content from '.$wpdb->prefix.'posts where id='.$id); $withlink=yes; $regenerate=yes; $align=left; $default=yes; $width=250; $height=190; $default_src='/thumbnail-for-excerpts/tfe_no_thumb.png'; $find=false; if(function_exists('has_post_thumbnail')) //you have WP 2.9 - do you use thumbnail feature, than I use it if(has_post_thumbnail($id)){ if($withlink=="yes") $plus='<a href="'.get_permalink().'">'.get_the_post_thumbnail($id,array($width,$height),array('class'=>'align'.$align.' tfe')).'</a>'; else $plus=get_the_post_thumbnail($id,array($width,$height),array('class'=>'align'.$align.' tfe')); $find=true; } if(!$find){ //no thumbnail defined with 2.9 feature... than we go the old way $post_thumbnail = tfe_get_image($id,$content,$align,$width,$height,$default,$regenerate,$default_src); if(!empty($post_thumbnail)) if($withlink=="yes") $plus='<a href="'.get_permalink().'">'.$post_thumbnail.'</a>'; else $plus=$post_thumbnail; } if(is_feed()) //this is going on feed, so it needs a simple formatting, no classes if($align=="center") $plus = '<p align="center">'.$plus.'</p>'; else $plus = str_replace('<img ','<img align="'.$align.'" hspace="5" ',$plus); return $plus.$excerpt; } add_filter("the_content","tfe_change_to_excerpts"); function tfe_change_to_excerpts($content){ //for single articles and full feeds, we don't want to force the excerpt if(is_single() || is_feed()) return $content; $on_home=no; $on_archives=yes; $on_search=yes; //cases for which to apply the force content->excerpt if((is_home()) || (is_archive()) || (is_search())){ global $post; $content = $post->post_excerpt; if($content) $content=apply_filters('the_excerpt', $content); else { $content = $post->post_content; $content = strip_shortcodes($content); $content = str_replace(']]>', ']]>', $content); $content = strip_tags($content); $excerpt_length = 55; $words = explode(' ',$content,$excerpt_length+1); if(count($words) > $excerpt_length){ array_pop($words); array_push($words,'...'); $content=implode(' ',$words); } $content = '<p>'.$content.'</p>'; } return tfe_put_image($content); } else return $content; } ?>
Hi there, Im not sure exactly what the problem is with your script, but that error is caused by either creating a function with the same name as another function, hence "cannot redeclare" or: The file that contains the tfe_get_image() function has been included / required more than once. It was first declared here: /home/nitendra/public_html/explorenikaya.com/wp-content/themes/WP-Genius/img.php on line 16 Then the script attempted to declare it again here : /home/nitendra/public_html/explorenikaya.com/wp-content/themes/WP-Genius/img.php on line 94 Check those places in you code, that should help you debug further.